アルゴリズム(四)
3527 ワード
時計回りの回転行列
[[1,2,3],[4,5,6],[7,8,9]],[7,4,1],[8,5,2],[9,6,3]]for i 0->n-1(列を表す)for j n-1->0(行を表す)a[i][n-j-1]=mat[j][i];
チェーンテーブルに返信があるかどうかを判断する
[[1,2,3],[4,5,6],[7,8,9]],[7,4,1],[8,5,2],[9,6,3]]for i 0->n-1(列を表す)for j n-1->0(行を表す)a[i][n-j-1]=mat[j][i];
public int[][] rotateMatrix(int[][] mat, int n) {
// write code here
int[][] a=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=n-1;j>=0;j--)
{
a[i][n-j-1]=mat[j][i];
}
}
return a;
}
チェーンテーブルに返信があるかどうかを判断する
//
public class PalindromeList {
public boolean chkPalindrome(ListNode A){
ListNode fast=A;
ListNode slow=A;
ListNode nextHead=null;
if(A==null||A.next==null)
{
return true;
}
// , fast.next.next!=null 。 fast ,fast.next 。fast.next.next 。 fast.next!=null && ,
while (fast.next!=null&&fast.next.next!=null)
{
fast=fast.next.next;
slow=slow.next;
}
nextHead=reverseList(slow.next);
while (nextHead!=null)
{
if(A.val==nextHead.val){
nextHead=nextHead.next;
A=A.next;
}else {
return false;
}
}
return true;
}
//
public ListNode reverseList(ListNode head)
{
if(head==null||head.next==null)
{
return head;
}
ListNode tail=null;
while (head!=null)
{
ListNode next=head.next;
head.next=tail;
tail=head;
head=next;
}
// tail
return tail;
}
}