C# solution that uses 3 traversals


  • 0
    K
    public class Solution {
        public void Rotate(int[] nums, int k) {
            if (k == 0) return;
            int l = nums.Length;
            int[] result = new int[l];
            
            k = k % l;
            for(int i = 0; i < l - k; i++){
                result[k + i] = nums[i];
            }
            for(int j = 0; j < k; j++)
            {
                result[j] = nums[l - k + j];
            }
            for(int p = 0; p < l; p++){
                nums[p] = result[p];
            }
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.