simple to understand code


  • 0
    X
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            int[] ans = new int[2];
            Map<Integer, Integer> map = new HashMap<>();
            
            for(int i = 0; i < nums.length; i++) {
                int num = target - nums[i];
                if(map.containsKey(num)) {
                    ans[0] = map.get(num);
                    ans[1] = i;
                    return ans;
                }
                
                map.put(nums[i], i);
            }
            return ans;
        }
    }
    

Log in to reply
 

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