C# solution shared using stack


  • 0
    D
    Stack<int> stack = new Stack<int>();
            if (k <= nums.Length)
            {
                for (int i = nums.Length - 1 - k; i >= 0; i--)
                {
                    stack.Push(nums[i]);
                }
                for (int i = nums.Length - 1, j = 1; j <= k; j++, i--)
                {
                    stack.Push(nums[i]);
                }
                int a = 0;
                while (stack.Count != 0)
                {
                    nums[a] = stack.Pop();
                    a++;
                }
            }
            else if(k>nums.Length)
            {
                k = k-nums.Length;
                Rotate(nums,k);
            }

Log in to reply
 

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