Why I received the wrong answer in OJ while correct answer in Eclipse


  • -2
    C

    And this is my code.

    public class Solution {
    public void rotate(int[] nums, int k) {
    int n = nums.length;// length n of the array

    	if(k < 0 || k == n) {
    		return;
    	}
    	
    	if(k > n) {
    		k = k % n;
    	}
    	
    	int[] newNums1 = new int[n - k];// new a new array for the first part of the array
    	int[] newNums2 = new int[k];// new a new array for the second part of the array
    	int[] newNums = new int[n];// new a new array for the rotated array
    	
    	for(int i = 0; i < n - k; i++) {
    		newNums1[i] = nums[i];
    	}
    	
    	for(int i = 0; i < k; i++) {
    		newNums2[i] = nums[n - k + i];
    	}
    	
    	for(int i = 0; i < k; i++) {
    		newNums[i] = newNums2[i];
    	}
    	
    	for(int i = k;i < n ; i++) {
    		newNums[i] = newNums1[i - k];
    	}    
        
    }
    

    }


  • 0
    X
    This post is deleted!

Log in to reply
 

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