力扣网解题

力扣网解题(一 两数之和)

题目如下:
力扣网解题_第1张图片
解题代码:

public class TwoSum {

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> record = new HashMap<>(16);
        for(int i = 0;  i<nums.length;i++){
            if(record.get(target - nums[i]) != null){
                return new int[]{record.get(target - nums[i]),i};
            }
            record.put(nums[i],i);
        }
        return null;
    }

    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        int target = 9;
        System.out.println(Arrays.toString(twoSum(nums, target)));
    }
}

你可能感兴趣的:(力扣解题,leetcode)