Accepted C# Solution


  • 0
    Y

    how to improve?

    public class Solution {
    public void Rotate(int[] nums, int k) {
            int[] result = new int[nums.Length];
            int n = nums.Length;
            
            for (int i = 0; i < nums.Length; i++)
            {
    
    
                result[(i+k)%n] = nums[i];
    
    
            }
            for(int i = 0; i < nums.Length; i++)
            {
                nums[i] = result[i];
            }
    }
    

    }


  • 0
    6
    private static void myRotateArrayNew(int[] nums, int k)
            {
                int n = nums.Length;
                int[] result = new int[n];
    
                // reduce compute time
                int rotateTime = k % n; 
                for (int index = 0; index < n; index++)
                {
                    result[(rotateTime +index)%n] = nums[index];
                }
    
                for (int index = 0; index < n; index++)
                {
                    nums[index]= result[index];
                }
            }

Log in to reply
 

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