两数之和(Java)

题目描述:

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

来源:力扣(LeetCode)



方法一:首先想到的一定是暴力破解法(代码如下)。

class Solution {

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

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

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

if(nums[i]+nums[j]==target){

return new int[]{i,j};

}

}

throw new IllegalArgumentException("No two sum solution");

}

}

方法二:可以将数组值与数组下标异一一对应,用map。

class Solution {

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

Map<Integer,Integer>map=new HashMap<>();

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

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

}

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

int complement=target-nums[i];

if(map.containsKey(complement)&&map.get(complement)!=i){

return new int[]{i,map.get(complement)};

}

}

throw new IllegalArgumentException("No two sum solution");

}

}

*map基本使用方法(见笔记)

原文地址:https://www.cnblogs.com/bitreea/p/12306219.html

时间: 2024-07-28 21:05:28

两数之和(Java)的相关文章

力扣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.

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

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

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 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 m

找出数组中两数之和为指定值的所有整数对

一,问题描述 给定一个整型数组(数组中的元素可重复),以及一个指定的值.打印出数组中两数之和为指定值的 所有整数对 二,算法分析 一共有两种方法来求解.方法一借助排序,方法二采用HashSet 方法一: 先将整型数组排序,排序之后定义两个指针left和right.left指向已排序数组中的第一个元素,right指向已排序数组中的最后一个元素 将 arr[left]+arr[right]与 给定的元素比较,若前者大,right--:若前者小,left++:若相等,则找到了一对整数之和为指定值的元素

leecode刷题(8)-- 两数之和

leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 思路: 这道题其实很简单,我们可以直接用暴力搜索的方法,设置双重

两数之和,三数之和,最接近的三数之和,四数之和

LeetCode有一系列做法套路相同的题目,N数之和就可以算一个 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 第一个解决办法,简单暴力,堆for循环就是,但

LeetCode题解001:两数之和

两数之和 题目 给定一个整数数组 nums?和一个目标值 target,请你在该数组中找出和为目标值的那?两个?整数,并返回他们的数组下标 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] Java: 方法一:暴力法 暴力法很简单,就是用两遍循环的方式遍历nums class Solution {

LeetCode-1两数之和

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