题目描述:
    image.png
    输入输出Demo:
    image.png
    解析:
    本题属于easy难度,只需要遍历一遍数组即可,也没有算法可言

    1. class Solution {
    2. public int[] twoSum(int[] nums, int target) {
    3. int[] res=new int[2];
    4. HashMap<Integer, Integer> map = new HashMap<>();
    5. for (int i = 0; i < nums.length; i++) {
    6. if(map.containsKey(target-nums[i])){
    7. res[0]=i;
    8. res[1]=map.get(target-nums[i]);
    9. }else{
    10. map.put(nums[i],i);
    11. }
    12. }
    13. return res;
    14. }
    15. }