LeetCode solutions by tgic

Rotate Array

https://leetcode.com/problems/rotate-array

Last Update 2015-03-03 17:39:57 +0000 View on Github

Source code Read on Github

 1 public class Solution {
 2     public void rotate(int[] nums, int k) {
 3         
 4         if(k == 0) return;
 5 
 6 
 7         int c = 0;
 8 
 9         for(int j = 0; j < k; j++) {
10             int i = j;
11             int t = nums[i];
12 
13 
14             for (;;) {
15 
16                 i = (i + k) % nums.length;
17 
18                 int nt = nums[i];
19 
20                 nums[i] = t;
21 
22                 t = nt;
23 
24                 c++;
25 
26                 if (i == j) break;
27             }
28 
29             if(c == nums.length) break;
30         }
31         
32     }
33 }
comments powered by Disqus

LeetCode solutions by tgic

  • LeetCode solutions by tgic
  • [email protected]
  • Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
  • tg123
  • tg123/leetcode

LeetCode java solutions by tgic