[Algorithm] Rotate 問題

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  6
7  8  9
1  4  7                  swap(matrix[i][j],matrix[j][i])
2  5  8
3  6  9
7  4  1                  swap(matrix[i][j],matrix[i][matrix[0].length-1-j])
8  5  2
9  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;
            }
        }
    }
}

發佈了102 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章