[Algorithm] Rotate 问题

来源:互联网 发布:怪物猎人3g数据库下载 编辑:程序博客网 时间:2024/06/06 14:13

Rotate String

Given a string and an offset, rotate string by offset. (rotate from left to right)

Example

Given "abcdefg".

offset=0 => "abcdefg"offset=1 => "gabcdef"offset=2 => "fgabcde"offset=3 => "efgabcd"
Challenge 

Rotate in-place with O(1) extra memory.

例如:char[] str=[a,b,c,d,e,f,g],offset=3

           Output:    [e,f,g,a,b,c,d]

三步翻转法:

1. [g,f,e,d,c,b,a],整个大翻转

2. [e,f,g,d,c,b,a], e,f,g翻转

3. [e,f,g,a,b,c,d], a,b,c,d翻转

注意:先整个大翻转的必要性,一般来说,由于往前放的位数少,因此先进行整个大翻转会省时

          当然也可以先局部翻转,再整个大翻转

          要看具体题目要求,是要在原来有序的数组上后面几个元素翻转到前面,还是已经翻转好的要恢复有序数组

          也就是说,是要[a,b,c,d,e,f,g]->[e,f,g,a,b,c,d],还是[e,f,g,a,b,c,d]恢复成[a,b,c,d,e,f,g]

public class Solution {    /**     * @param str: an array of char     * @param offset: an integer     * @return: nothing     */    public void rotateString(char[] str, int offset) {        if(offset==0){            return;        }                if(str==null || str.length==0){            return;        }                int n=str.length;        offset=offset%n;        reverse(str,0,n-1);        reverse(str,0,offset-1);        reverse(str,offset,n-1);    }        private void reverse(char[] str,int start,int end){        while(start<end){            char temp=str[start];                      //char temp            str[start]=str[end];                                   str[end]=temp;            start++;            end--;        }    }}

Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative.

Example

Given 1->2->3->4->5 and k = 2, return 4->5->1->2->3.

思路:先遍历一遍整个链表得到整个链表的长度n,然后把链表的最后一个节点和链表的头节点相连,

          再向后走n-k%n个断开链表,使得断开的链表的头成为newhead

注意:k长度大于链表长度,甚至k长度远大于链表长度,要k%n

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode rotateRight(ListNode head, int k) {        if(head==null){            return null;        }        if(k==0){            return head;        }                ListNode cur=head;        int n=1;        while(cur.next!=null){            n++;            cur=cur.next;        }        cur.next=head;                int index=n-k%n;        for(int i=0;i<index;i++){            cur=cur.next;        }        ListNode newhead=cur.next;        cur.next=null;        return newhead;           }}

Rotate Image

You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).

Example

Given a matrix

[    [1,2],    [3,4]]

rotate it by 90 degrees (clockwise), return

[    [3,1],    [4,2]]
Challenge 

Do it in-place.

思路:顺时针90==两次翻转,同理可以推逆时针90

1  2  3             4  5  67  8  9
1  4  7                  swap(matrix[i][j],matrix[j][i])2  5  83  6  9
7  4  1                  swap(matrix[i][j],matrix[i][matrix[0].length-1-j])8  5  29  6  3

注意:do it in-place

public class Solution {    /**     * @param matrix: A list of lists of integers     * @return: Void     */    public void rotate(int[][] matrix) {        for(int i = 0; i<matrix.length; i++){            for(int j = i; j<matrix[0].length; j++){                int temp = matrix[i][j];                matrix[i][j] = matrix[j][i];                matrix[j][i] = temp;            }        }        for(int i =0 ; i<matrix.length; i++){            for(int j = 0; j<matrix[0].length/2; j++){                int temp = matrix[i][j];                matrix[i][j] = matrix[i][matrix[0].length-1-j];                matrix[i][matrix[0].length-1-j] = temp;            }        }    }}