问题描述
Given an array of integers, 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 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
原问题链接:https://oj.leetcode.com/problems/two-sum/
问题分析
这个问题相对来说比较简单,在给定一组数字的时候,我们需要去找两个数字之和为给定数字的一个组合。所以从最开始来说,一个最简单直接的方法就是二重循环遍历数组,找到匹配的就返回。这种方法的实现如下:
public int[] twoSum(int[] numbers, int target) { int[] result = new int[2]; for(int i = 0; i < numbers.length; i++) { for(int j = i + 1; j < numbers.length; j++) { if(numbers[i] + numbers[j] == target) { result[0] = i; result[1] = j; return result; } } } return null; }
这个办法非常的简单直观,不过从性能角度来说却不行。它的时间复杂度达到了O(N^2) 。那么,有没有办法将这个办法改进,以使得它的性能提升呢?
这个时候,我们会考虑到另外一个办法,就是原来的方法里是盲目的一个个的去比较和查找。如果有一个办法可以让我们有常量的时间内去查找对应的值是否存在,这样效率就会提升更多。于是,我们可以考虑用map的数据结构。这样我们一次遍历就解决问题了,每次我们遍历的时候就去判断map.containsKey(target - numbers[i]),如果有,则表示前面已经存在一个相加等于target的元素了。没有的话则将该元素加入到map里。按照这个思路,详细的代码实现如下:
public class Solution { public int[] twoSum(int[] numbers, int target) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int[] twoIndex = new int[2]; for(int i = 0; i < numbers.length; i++) { if(map.containsKey(target - numbers[i])) { twoIndex[1] = i + 1; twoIndex[0] = map.get(target - numbers[i]) + 1; return twoIndex; } map.put(numbers[i], i); } return null; } }
代码的实现比较简单。就不赘述了。里面的一些细节还是值得思考的。
在有的时候,我们开始会这么想。既然要查找一个数字是否有对应的target-number[i]的数字存在。那么我们首先把所有的数字都放到一个HashMap里,然后再一个个的去查这样也可以啊。这样会存在的一个问题就是如果我们取到一个值a[i],而target-a[i]的值正好也等于a[i]的时候,我们没法判断这是不是不同的两个元素的和。而且如果最开始的数组里有重复的元素也会是一个麻烦。
相关推荐
leetcode leetcode练习 twosum 问题 ;add two numbers问题;reverse integer问题;最大不重复子字符串长度问题;atoi问题;
leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two Sum #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:...
leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 $number 题号代表经典 LeetCode 题目,$number.$number 题号表示 LeetCode 面试题。 :receipt: 目录 统计 值 AC 的...
Leetcode\TwoSum\TwoSum.cs 问题: 业绩报告: 反转整数 代码: Leetcode\ReverseInteger\ReverseInteger.cs 问题: 业绩报告: 回文数 代码: Leetcode\PalindromeNumber\PalindromeNumber.cs 问题: 从排序数组中...
Leetcode two sum java 解法
Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 LeetCode 3 Longest Substring Without Repeating Characters LeetCode 13 Roman to...
leetcode打不开Leetcode Note Tips Tip1: Two pointer for sorted array (#Array 1. Two Sum) Tip2: Sum[i:j] = Sum[0:j] - Sum[0:i] for continuous array (# Array 560. Subarray Sum Equals K) Tip3: Knapsack ...
https://leetcode-cn.com/problems/two-sum/ 难度 : 简单 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。...
leetcode 1004 leetcode E:简单,M:中等,H:困难 数组和字符串 217. Contains Duplicate (E) 48. Rotate Image (M) -> 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E)...
leetcode 答案 leet code 这是我的leetcode答案,...Sum Add Two Numbers Longest Substring Without Repeating Characters Median of Two Sorted Arrays Longest Palindromic Substring Regular Expression Matching
leetcode 2 #LeetCode 解题记录 ##开始时间 2016年1月14日晚 ##目标 使用C++和python完成所有题目,并且排名全部在90%之前。 ###2016年1月14日晚 ####题号1: two sum C++: 击败 97.85% python : 未完成 ###2016年...
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...
leetcode下载 algorithm_reearch algorithm_reearch 链表 leetcode: 146 有无环 双向链表,删除 写大于读的情形 LinkedHashMap与 LRU算法 基于访问时间的链表 或者 基于插入时间的链表 哈希表: leetcode: 1. two sum...
lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. ...
Sum 这题是给你一个整数的数组,然后给你一个目标值,让你从这个数组中找出两个数相加等于这个目标值的数组索引值(不能是同一个元素)。 第一种解题思路就是两个for循环,第二个for循环的起始变量比第一个for循环...
Sum 1.指针的用法 需要通过函数改变外部非全局变量时,需要在形参里用指针声明,然后在函数内用*a=2的方式操作 2.Code::Blocks报错,leetcode服务器通过,未知原因 3.leetcode要求函数返回数组用malloc,例int* ret ...
Sum JavaScript O(n * log n) / O(n) O(1) / O(n) easy 2 Add Two Numbers JavaScript O(n) O(1) Medium 4 Median of Two Sorted Arrays JavaScript O(log (m+n)) O(1) Hard 7 Reverse Integer JavaScript O(n) O(1)...
Sum 滑动窗口需要记录 Leetcode Java Python Leetcode.3 Longest Substring Without Repeating Characters Leetcode.76 Minimum Window Substring Leetcode.438 Find All Anagrams in a String Pattern: two points ...
#leetcode 001_Medium: Two Sum.利用哈希表(在O(1)时间复杂度内查找某个元素)。遍历数组,查找map里是否containsKey(target-nums[i]),如果则返回,否则将nums[i]放入map中(map.put(nums[i],i)。 002_Medium: Add ...