JAVA Sorting by Counting (Ask for help)


  • 0
    A

    here is my code
    '''
    public void sortColors(int[] nums) {
    int len = nums.length;
    int[] count = new int[3];
    int[] res = new int[len];
    for(int i=0;i<len;i++) {
    if(nums[i]==0) {
    count[0]++;
    }
    else if(nums[i]==1) {
    count[1]++;
    }
    else{
    count[2]++;
    }
    }
    for(int i=1;i<count.length;i++) {
    count[i]+=count[i-1];
    }
    for(int i=len-1;i>=0;i--) {
    int position = count[nums[i]];
    res[position-1] = nums[i];
    count[nums[i]]--;
    }
    nums = res;
    }
    The test showed when input is 1,0,my output is 1,0. However, I did multiple tests in my IDE, the resulkt is correct,i,e 0,1.
    Who can help me figure out the reason why I can not pass the test.


Log in to reply
 

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