算法练习1.Two Sum 数组两个和为目标值的下标(map)
来源:互联网 发布:将电脑网络共享给手机 编辑:程序博客网 时间:2024/05/17 01:49
Two Sum
https://leetcode.com/problems/two-sum/
给定一个目标整数target,找出数组中和为target的两个数的下标
C++:class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> solve;
for(int i = 0 ; i < nums.size()-1;i++)
{
for(int j = i+1;j<nums.size();j++)
{
if(nums[i]+nums[j]==target)
{
solve.push_back(i);
solve.push_back(j);
return solve;
}
}
}
return solve;
}
};
以下内容转自LeeCode官网
Approach #1 (Brute Force) [Accepted]
The brute forceapproach is simple. Loop through each element xx and findif there is another value that equals to target - xtarget−x.
publicint[]twoSum(int[] nums, int target) {
for(int i=0; i< nums.length; i++) {
for (int j= i+1; j< nums.length; j++) {
if (nums[j] == target- nums[i]){
return newint[]{ i, j};
}
}
}
thrownew IllegalArgumentException("No two sum solution");
}
Approach #2 (Two-pass Hash Table)[Accepted]
To improve our run time complexity, we need a more efficient wayto check if the complement exists in the array. If the complement exists, weneed to look up its index. What is the best way to maintain a mapping of eachelement in the array to its index? A hash table.
We reduce the look up time from O(n)O(n) to O(1)O(1) bytrading space for speed. A hash table is built exactly for this purpose, itsupports fast look up in nearconstant time. Isay "near" because if a collision occurred, a look up coulddegenerate to O(n)O(n) time. Butlook up in hash table should be amortized O(1)O(1) time aslong as the hash function was chosen carefully.
A simple implementation uses two iterations. In the firstiteration, we add each element's value and its index to the table. Then, in thesecond iteration we check if each element's complement (target - nums[i]target−nums[i]) exists in the table. Beware that thecomplement must not be nums[i]nums[i] itself!
publicint[]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");
}
Approach #3 (One-pass Hash Table)[Accepted]
It turns out we can do it in one-pass. While we iterate andinserting elements into the table, we also look back to check if currentelement's complement already exists in the table. If it exists, we have found asolution and return immediately.
publicint[]twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
- 算法练习1.Two Sum 数组两个和为目标值的下标(map)
- 1. Two Sum 给定一个数组和一个数,输出两个和为给定数的数组元素的下标
- 【算法】求数组中某两个数的和为目标值
- 167. Two Sum II - Input array is sorted (在有序数组中确定和为给定值的两个元素的下标)
- leetcode_167. Two Sum II - Input array is sorted 有序数组中找和为特定数的两个数的下标
- leetcode 1 Two Sum(在无序数组中找两个数之和与目标值相等)
- 无序数组中找出和为N的两个数 Two Sum
- Two sum 在数组中寻找和为指定值的两个数
- [Leetcode #1]Two Sum 从数组中找出和为特定值的两个数
- 1.Two Sum (数组中两个数之和为n)
- leetcode 1 Two Sum(在无序数组中找两个数之和与目标值相等,两种方法)
- Two Sum 数组中找两个之和为sum@LeetCode
- 返回两个求和等于给定值的数组下标--算法练习
- Two Sum 从数组中找到两个位置的值相加和为给定目标的值
- LeetCode题解--1. Two Sum(和为S的两个数字)
- 一个数组中两个数的和为N,找出这两个数字的下标
- 3sum和4sum(从数组中选3/4个数,使其和为目标值)
- 面试算法题:数组中两个数之和为定值,找出这对数的下标
- 二维数组中的查找
- 超级简单的批量提取文件夹内文件名的经典方法
- 第一次
- 今日新闻
- 练习三1010
- 算法练习1.Two Sum 数组两个和为目标值的下标(map)
- 编译动态链接库.so和静态链接库.a
- 4289: PA2012 Tax
- iOS - 生成二维码
- Django项目部署 Django + nginx + tornado
- MySQL语句大全:创建、授权、查询、修改
- wpe 网络改包
- c3p0的使用
- 练习三1013