给定一个整数数组 nums 和一个整数目标值 target请你在该数组中找出 和为目标值 的那 两个 整数并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是数组中同一个元素不能使用两遍。你可以按任意顺序返回答案。public static int[] twoSum(int[] nums, int target) { int[] indexs new int[2]; // 建立k-v 一一对应的哈希表 HashMapInteger,Integer hash new HashMapInteger,Integer(); for(int i 0; i nums.length; i){ if(hash.containsKey(nums[i])){ indexs[0] i; indexs[1] hash.get(nums[i]); return indexs; } // 将数据存入 key为补数 value为下标 hash.put(target-nums[i],i); } return indexs; } }来源力扣LeetCode链接: https://leetcode-cn.com/problems/two-sum.著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。
阅读完成 · 觉得有帮助?