11 lines and 1 for in Java


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

  • 0
    Z
    This post is deleted!

Log in to reply
 

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