Rotate Array Why is always RunTime Error ?????????????????


  • 0
    P

    below is my code, It runs well in CodeBlocks ,but in OJ Always shows " Runtime Error " Who can help me ?why?

    class Solution
        {
        public:
            void rotate(int nums[], int n, int k)
            {
                k = k % n;
                if(k == 0 || n == 1) return;
                int tem[n];
                int j = 0;
                for(int i = n - k; i < n; ++i,++j)
                    tem[j] = nums[i];
                for(int i = 0; i <= k ; i++,j++)
                    tem[j] = nums[i];
               for(int i = 0; i < n; ++i)
                    nums[i] = tem[i];
            }
        
        };

  • 0
    T

    In the second for loop, the condition

    i <= k

    should be changed to

    i < n-k.

    The other thing is, why ++i in the first and third loop, but i++ in the second loop? I'd rather to have some consistency in style.


Log in to reply
 

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