C# solution: reverse


  • 0
    B
    public class Solution 
    {
        public void Rotate(int[] nums, int k) 
        {
            if (k == 0) return;
    
            k %= nums.Length;
    
            Reverse(nums, 0, nums.Length - 1);
    
            Reverse(nums, 0, k - 1);
            Reverse(nums, k, nums.Length - 1);
        }
    
        private void Reverse(int[] nums, int left, int right)
        {
            while(left < right)
            {
                var temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
    
                left++;
                right--;
            }
        }
    }
    

Log in to reply
 

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