首页 > 其他 > 详细

力扣-两数之和

时间:2021-02-22 17:12:47      阅读:24      评论:0      收藏:0      [点我收藏+]

题目描述:

技术分享图片

 1 class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         int[] indexs = new int[2];
 4         //创建hashmap
 5         HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();
 6         for(int i = 0; i < nums.length; i++){
 7             if(hash.containsKey(nums[i])){
 8                 indexs[0] = i;
 9                 indexs[1] = hash.get(nums[i]);
10                 return indexs;
11             }
12             // 两数查存入 ,value为下标
13             hash.put(target-nums[i],i);
14         }
15         return indexs;
16     }
17 }

 

力扣-两数之和

原文:https://www.cnblogs.com/buhaohedeyouzicha/p/14430700.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!