【LeetCode 1】算法修炼 --- Two Sum
来源:互联网 发布:知世俗而不世俗 编辑:程序博客网 时间:2024/04/28 01:30
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
Question Tags:
Array , Hash Table
New Words:
add up to:总计达
indices:index的复数
zero-based:从零开始的
Solution Ideas:
思路一:
两层遍历法:对于数组中的某一个数,对它及他以后的某个数求和,若和与target相等,则可确定这两值为所找的。此方式时间复杂度为O(nlogn).
思路二:
HashMap--Value-key法:求a+b=target,也就是判断a和target-a是否都在这个数组中,
遍历判断map中是否有数组中的某个值target-a,如果没有,则把a的key以value作为key存到map中,
如果有,则所求的a,b得出来了。所求的索引值也就是a,b的索引值
此方法时间复杂度为O(n)
两种方法都可以确保index1<index2.
只考虑时间复杂度的情况下,由O(n)<O(nlogn)知,思路二的效率更高。
Solution Java code:
import java.util.Arrays;import java.util.HashMap;import java.util.Map;public class TwoSum { public static void main(String[] args) { int[] numbers={2, 7, 11, 15}; int target = 9; int[] twoSum = twoSum(numbers,target); System.out.println("two sum indices are " + twoSum[0] + " and " + twoSum[1]); int[] twoSum2 = twoSum2(numbers,target); System.out.println("two sum indices are " + twoSum2[0] + " and " + twoSum2[1]); }
//思路1 public static int[] twoSum(int[] numbers, int target) { int i,j,sum; int[] indices = new int[2]; outfor:for (i=0;i<numbers.length;i++){ for (j=i+1;j>i && j<numbers.length;j++){ sum = numbers[i]+numbers[j]; if (sum == target){ indices[0]=i+1; indices[1]=j+1; break outfor; } } } return indices; }
//思路2 public static int[] twoSum2(int[] numbers, int target) { int[] indices = new int[2]; Map<Integer,Integer> map = new HashMap<Integer,Integer>(); for (int i=0;i<numbers.length;i++){ if (map.containsKey(target-numbers[i])){ indices[0]=map.get(target-numbers[i]); indices[1]=i+1; break; } map.put(numbers[i],i+1); } return indices; }}
思路2也可以使用hash table表达:
public static int[] twoSum3(int[] numbers, int target) { int[] indices = new int[2]; // Map<Integer,Integer> map = new HashMap<Integer,Integer>(); Hashtable<Integer, Integer> hashtable = new Hashtable<Integer, Integer>(); for (int i=0;i<numbers.length;i++){ Integer num = hashtable.get(numbers[i]); if (num == null) hashtable.put(numbers[i], i); num = hashtable.get(target-numbers[i]); if ( num != null && num < i) { indices[0] =num + 1; indices[1] = i+1; return indices; } } return indices; }
- 【LeetCode 1】算法修炼 --- Two Sum
- 【LetCode算法修炼】Two Sum
- LeetCode的算法题1:Two Sum
- leetcode算法训练#1 Two Sum
- Leetcode算法题(1)Two Sum
- LeetCode算法(Python)--1、Two Sum
- LeetCode 1 - Two Sum
- leetcode 1 Two Sum
- Leetcode【1】:Two Sum
- [leetcode 1] Two Sum
- 【leetcode-1】Two Sum
- [leetcode 1] Two Sum
- [Leetcode] 1 - Two Sum
- LeetCode (1) Two Sum
- LeetCode 1:《Two Sum》
- LeetCode | #1 Two Sum
- leetcode-1 Two Sum
- Two Sum | LeetCode(1)
- 【Mood-17】 github中在本地进行上传的时候出现ERROR: Repository not found. fatal: The remote end hung up unexpectedly
- 【Mood-18】github 使用指南
- 【Android Studio使用教程5】使用SDK Manager时, SDK下载更新不了的解决方案(eclipse 也适用)
- 【Android Studio使用教程6】Execution failed for task ':×××:compileReleaseAidl'
- 工厂三兄弟之工厂方法模式(一):日志记录器的设计
- 【LeetCode 1】算法修炼 --- Two Sum
- 【Linux/Ubuntu学习 12】ubuntu下对/etc/profile误修改导致系统不能登录
- POJ-1887 Testing the CATCHER(dp,最长下降子序列)
- vmware中正在决定 eth0 的 IP 信息...失败;无链接。检查电缆吗?问题的解决方法
- 【Android Studio使用教程 7】AndroidStudio问题汇总
- 【Linux/Ubuntu学习 13】ubuntu上好用的pdf软件okular
- 【Linux/Ubuntu学习 14】Linux下查看文件和文件夹大小
- nyoj 10 skiing 【记忆化】
- 【HTML5 1】39个让你受益的HTML5教程