Is this Algorithm optimal or what?


  • 0
    G
    	public int[] twoSum(int[] nums, int target) {
    		int length = nums.length;
    		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    		map.put(nums[0] , 0 );
    		for(int i=1 ; i< length ; i++){
    			int num = nums[i];
    			if(map.containsKey(target-num)){
    				return new int[]{i, map.get(target-num)};
    			}
    			map.put(num , i );
    		}
    		throw new IllegalArgumentException("Now tow sum solution");
    	}
    

Log in to reply
 

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