public void rotate(int[]nums, int k) { int n = nums.length; int[] newNums = new int[n]; k = k % n;// k is number of rotations for(int i = 0; i < n; i++) { newNums[(i + k) % n] = nums[i]; } for(int i = 0; i < n; i++){ nums[1] = newNums[i]; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter