[LeetCode] Rotate Array / List

Question:

http://leetcode.com/2010/04/rotating-array-in-place.html

Rotate a one-dimensional array of n elements to the right by k steps. 

For instance, with n=7 and k=3, the array {a, b, c, d, e, f, g} is rotated to {e, f, g, a, b, c, d}

// O (n)
public void rotate(int[] A, int k)
{
    // Assumptions:
    // A not null
    // A not empty
    // if k > 0, rotate >>,
    // if k < 0, rotate <<.
    //
    // rotatedIndex = ( originalIndex + k ) % len
    
    int len = A.length;
    
    int original = 0;
    int from = A[0];
    do
    {
        // Calculate rotated index
        int rotated = (original + k) % len;
        
        // Assign expected value to rotated.
        int temp = A[rotated];
        A[rotated] = from;
        
        // Save expected index for next rotated value.
        from = temp;
        original = rotated;
    }
    while (original != 0);
}

What if Rotate a list?

http://7371901.blog.51cto.com/7361901/1598840

// O (n)
public ListNode rotate(ListNode head, int k)
{
    if (head == null || head.next == null || k == 0)
        return head; // No need further
        
    int size = 1;
    ListNode tail = head;
    while (tail.next != null)
    {
        tail = tail.next;
        size ++;
    }
    
    // Re-calculate k in case k is negative.
    k = ((k % size) + size) % size;
    int newTailIndex = size - 1 - k;
    ListNode newTail = head;
    for (int i = 0 ; i < newTailIndex ; i ++)
    {
        newTail = newTail.next;
    }
    ListNode newHead = newTail.next;
    newTail.next = null;
    tail.next = head;
    return newHead;
}
时间: 2024-12-28 02:36:17

[LeetCode] Rotate Array / List的相关文章

2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 思路: 我的思路:新建一个数组存放旋转后的内容,但是怎么把原数组的内容存放在数组中,不清楚. leetcode/discuss思路: 思路一:新建数组,复制原

LeetCode Rotate Array 翻转数组

题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. 1 class Solution { 2 public: 3 void rotate(int nums[], int n, int k) { 4 if( !k || !n || n==1 || k==n ) return; 5 k %= n; 6 vector<int> cha; 7 cha.reserve

[LeetCode] Rotate Array

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro

[LeetCode] Rotate Array 旋转数组

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro

[LeetCode]189.Rotate Array

题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note: Try to come up as many solutions as you can, there are at least 3 different ways to solve this

LeetCode:(Array-189) Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

LeetCode: Reverse Words in a String &amp;&amp; Rotate Array

Title: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". https://leetcode.com/problems/reverse-words-in-a-string/ 思路:先将字符串全部逆转,在将每个单词逆转.具体实现,要注意空格,所以对字符串倒过来处理. cla

LeetCode:Rotate Array

1.题目名称 Rotate Array(平移数组) 2.题目地址 https://leetcode.com/problems/rotate-array/ 3.题目内容 英文:Rotate an array of n elements to the right by k steps. 中文:将一个长度为n的数组,向右平移k个单位 4.解题方法1 一个比较易于理解的方法是新开辟一个与原数组等长的数组,循环考察原数组的元素,将它们放到新数组中平移后的位置上,最后再将新数组上的元素赋回原数组. 一段可以

【LeetCode】189. Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s