package com.zmz.algorithm.hashtable;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @author 张明泽
 * Create by 2022/5/30 19:29
 * 两数之和
 * LeetCode-1
 */
public class SumTwoNumber {
    public static void main(String[] args) {
        int[] nums = {2,7,11,15};
        int[] result = getSum(nums,9);
        System.out.println(Arrays.toString(result));
    }
    public static int[] getSum(int[] nums, int target) {
        HashMap<Integer,Integer> hashMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (hashMap.containsKey(nums[i])) {
                return new int[] {hashMap.get(nums[i]),i};
            }
            hashMap.put(target - nums[i], i);
        }
        return null;
    }
}
最后修改:2022 年 06 月 02 日 10 : 01 AM
赏杯咖啡喝 谢谢您~