[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;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值