[leetcode]51 Two Sum
来源:互联网 发布:Date java 编辑:程序博客网 时间:2024/06/16 07:34
题目链接:https://leetcode.com/problems/two-sum/
Runtimes:33ms
1、问题
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
2、分析
这道题目遇到过,不过那是两个数组,求是否存在两个数分别在两个数组中,相加等于target。最传统的做法是两两进行一次配对,找到则返回,时间复杂度为O(n^2),方案1给出了时间复杂度;
根据提示,用了map数据结构,查找时间用了O(log2^n),每个数字遍历一遍,时间复杂度为O(n + n * log2^n);但是跑出来的时间还是很大,至少不在c++程序跑的时间范围内。有待改进。
思路三对思路二进行了一次优化,省去了好多不必要的操作,学习了,赞!
3、小结
一些测试案例取的不是很好,比如{-1, -2, -3, -4, -5},-8;再比如{0, 1, 2, 3, 0},0;等等。思维要严谨。
熟悉并总结map的使用,方案3中,利用了map中如果没有所要查找的元素的话,map[X]返回0的做法,值得赞。可见对一个stl类的熟练程度也是可以优化代码的。
4、实现
方案1:
class Solution {public: vector<int> twoSum(vector<int> &numbers, int target) { vector <int> result; if (numbers.size() <= 1) return result; for (int i = 0; i < numbers.size() - 1; ++i) { for (int j = i + 1; j < numbers.size(); ++j) { if (numbers[i] + numbers[j] == target) { result.push_back(i); result.push_back(i); return result; } } } return result; }};
方案2:
class Solution {public: vector<int> twoSum(vector<int> &numbers, int target) { vector <int> result; if (numbers.size() <= 1) return result; map <int, int> m, m1; for (int i = 0; i < numbers.size(); ++i) { if (m.find(numbers[i]) != m.end()) m1.insert(map<int, int>::value_type(numbers[i], i)); m.insert(map<int, int>::value_type(numbers[i], i)); } for (int i = 0; i < numbers.size(); ++i) { int tmp = target - numbers[i]; map<int, int>::iterator iter = m.find(tmp); if (iter != m.end() && (iter->second != i ||(iter->second == i && m1.find(tmp) != m1.end()))) { result.push_back(i + 1); if (iter != m.end() && iter->second != i) result.push_back(iter->second + 1); else result.push_back(m1.find(tmp)->second + 1); return result; } } return result; }};
方案3
class Solution {public: vector<int> twoSum(vector<int> &numbers, int target) { vector <int> result; if (numbers.size() <= 1) return result; map <int, int> m; int find; for (int i = 0; i < numbers.size(); ++i) { find = m[target - numbers[i]]; if (find) { result.push_back(find); result.push_back(i + 1); return result; } m[numbers[i]] = i + 1; } return result; }};
5、反思
参考一篇文章后,速度提高了33ms,达到平均水平,赞。
文章链接:http://www.acmerblog.com/leetcode-two-sum-5223.html
- [leetcode]51 Two Sum
- 【Leetcode】Two Sum (Sum)
- LeetCode: Two Sum
- LeetCode: Two Sum
- [Leetcode] Two Sum
- LeetCode : Two Sum
- Leetcode: Two Sum
- leetcode 46: Two Sum
- [LeetCode] Two Sum
- LeetCode 1 - Two Sum
- Leetcode : Two Sum
- 【leetcode】Two Sum
- [LeetCode]Two Sum
- leetcode Two Sum
- [Leetcode] Two Sum
- [leetcode] Two Sum
- LeetCode - Two Sum
- leetcode之Two Sum
- OC学习之道:OC中类别--Category类目的使用/Eetension类的扩展 的使用
- Python中如何遍历字典
- 程序的内存分配
- 条款04:确定对象被使用前已被初始化
- c++排序(快速排序)
- [leetcode]51 Two Sum
- COGS-313-和平委员会-POI2001-2-SAT
- 使用JPA进行数据操作
- java中Math.random()与java.util.random()的区别
- c++ template(9)trait和Policy
- Syntax error on token *,invalid variabledeclaratorid
- c++ template(8)模版多态
- Android 打飞机小游戏 cocos2d
- 提权广告件PermAd分析报告