Can somebody see where I'm going wrong with this Java solution?


  • 0
    A

    It seems pretty solid to me and even runs fairly quick with very large numbers on my IDE. Sorry if it's really obvious, I'm still learning.

     public class Solution {
            public void rotate(int[] nums, int k) {
                k = k%nums.length;
                for (int i = 0; i < k; i++) {
                    int tmp = nums[nums.length-1];
                    for (int j = nums.length-1; j > 0; j--)
                        nums[j] = nums[j-1];
                    nums[0] = tmp;    
                }
            }
        }

  • 0
    J

    there are O(nums.lenght) time algorithm. you algorithm is O(nums. length * k). when nums.length is very big and k is near to nums.length, you algorithm will be very slow.


Log in to reply
 

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