力扣01两数之和Java版

class Solution {

public int[] twoSum(int[] nums, int target) {

HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();

int[] res = new int[2];

for (int i = 0; i < nums.length; ++i) {

if (m.containsKey(target - nums[i])) {

res[0] = m.get(target - nums[i]);

res[1] = i;

break;

}

m.put(nums[i], i);

}

return res;

}

}

1.   class Solution {

2.      public int[] twoSum(int[] nums, int target) {

3.          HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();

4.          int[] res = new int[2];

5.          for (int i = 0; i < nums.length; ++i) {

6.              if (m.containsKey(target - nums[i])) {

7.                  res[0] = m.get(target - nums[i]);

8.                  res[1] = i;

9.                  break;

10.            }

11.            m.put(nums[i], i);

12.        }

13.        return res;

14.    }

15.}

原文地址:https://www.cnblogs.com/codeleader/p/11634139.html

时间: 2024-07-30 11:35:57

力扣01两数之和Java版的相关文章

LeetCode刷题 - (01)两数之和

题目描述 给定一个整数数组nums和一个目标值target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 解法一 暴力解法 解题思路 最容易想到的就是暴力法,使用两个遍历,查找两数之和是否为target.流程如下: 使用i来遍历

两数之和(Java)

题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1] 来源:力扣(LeetCode) 方法一:首先想到的一定是暴力破解法(代码如下). class Solution 

leetcode 01两数之和

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1] 暴力法: class Solution {public:    vector<int> twoSum(vector<int>& nums, int target) {       

[leetcode题解]01两数之和

给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 分析 关键字:数组.查找.下标. 由于数组是无序的,查找必须要遍历,不能用二分查找,时间复杂度一般最小为O(n). 暴力解法:遍历

LeetCode01 - 两数之和(Java 实现)

LeetCode01 - 两数之和(Java 实现) 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/two-sum 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + n

leetCode:twoSum 两数之和 【JAVA实现】

LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:twoSum 两数之和 [JAVA实现] 方法一 使用双重循环两两相加判断是否等于目标值 public List<String> twoSum2(int[] arr, int sum) { if (arr == null || arr.length == 0) { return new ArrayL

LeetCode-1两数之和

问题: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/two-sum著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 分析: 该题核心是查找,那么和查找有关的,我们优先想到的就是通过hashmap来进行.那么通

【Leetcode】【简单】【1. 两数之和】【JavaScript】

题目描述 1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1] 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problem

LeetCode | No.1 两数之和

题目描述: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. 给定一个整数数组nums和一个目标值target,请你在该数