The wrong with "Find the Duplicate Number"


  • 0
    B

    In this question, I use

    public class Solution {
        public int findDuplicate(int[] nums) {
            int a[] =new int[nums.length-1];
    
    for(int i=0;i<nums.length;i++){
    			a[nums[i]-1] +=1;
    			if(a[nums[i]-1]==2){
    					return nums[i];
    				}
        	}
        	return 0;
        }
    }
    

    this function and pass the exam.But,the extra space is not O(1).


Log in to reply
 

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