[c++] LeetCode Two Sum问题
来源:互联网 发布:linux修改文件读写权限 编辑:程序博客网 时间:2024/05/21 09:43
谢谢师兄的推荐,LeetCode点击打开链接很适合来刷题,初步着手做编程练习题,下面是算法题第一题two sum问题的描述:
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
开始用两层for循环遍历,运行时超过时间限制,从前写程序从来没有考虑过运行时间的问题,也算是涨了见识了,把LeetCode discuss部分投票高的答案搬过来,供大家参考吧。
vector<int> twoSum(vector<int> &numbers, int target){ //Key is the number and value is its index in the vector. unordered_map<int, int> hash; vector<int> result; for (int i = 0; i < numbers.size(); i++) { int numberToFind = target - numbers[i]; //if numberToFind is found in map, return them if (hash.find(numberToFind) != hash.end()) { //+1 because indices are NOT zero based result.push_back(hash[numberToFind] + 1); result.push_back(i + 1); return result; } //number was not found. Put it in the map. hash[numbers[i]] = i; } return result;}
读入一个数numbers[i],根据和target的关系得到目标数numberToFind,通过unordered_map hash快速找有没有,没有将numbers[i]添加到hash中,继续读下一个数。
程序最坏的结果是要找得两个数位于数组的末尾,没有学过算法和数据结构的知识,相比于两层for循环,应该是hash查找的速度更快,所以没有出现超时。
另一个答案:
class Solution{public: vector<int> twoSum(vector<int>& numbers, int target) { vector<int> tmpNumbers(numbers.begin(), numbers.end()); sort(tmpNumbers.begin(), tmpNumbers.end()); int val1 = -1; int val2 = -1; int i = 0; int j = tmpNumbers.size() - 1; // find two numbers added equals to target while(i < j) { if((tmpNumbers[i] + tmpNumbers[j]) < target) { i++; } else if((tmpNumbers[i] + tmpNumbers[j]) > target) { j--; } else { val1 = tmpNumbers[i]; val2 = tmpNumbers[j]; break; } } vector<int> result; // find the index of the two numbers for(int i = 0; i < numbers.size(); i++) { if(numbers[i] == val1 || numbers[i] == val2) { result.push_back(i + 1); } if(2 == result.size()) { return result; } } return result; }};
首先将数组排序(时间空间复杂度不会分析)得到temNumbers,然后分别从temNumbers的首尾开始找,根据当前首尾值相加和target比较,决定首部或尾部向中间移动一步,找到符合要求的值后再在原数组中找到对应的序号。
总结:
1. 末尾元素用下标表示为nums[nums.size()-1],迭代器是 *(nums.end()-1)
2. 第二种方法找到对应numbers下标,有几个错误的方法1)用两遍循环找,如果val1==val3,出错,2)一遍循环找到val1后从下一个开始找val2对应的序号,如果val1出现在末尾也出错,解决方法
一个从左往右,一个右往左,左后确定一下大小关系,swap
另一种方式是if(nums[i] ==val1 || nums[i] == val2) 但需要判读两个是否都找到了
- [c++] LeetCode Two Sum问题
- LeetCode two sum问题
- [C++]LeetCode: 14 Two Sum
- Leetcode[1] Two Sum (c++)
- LeetCode::Two Sum C语言
- [LeetCode#1][C]Two Sum
- 【LeetCode-1】 Two Sum(C++)
- LeetCode Two Sum(C/JS)
- LeetCode 1. Two Sum (C++)
- LeetCode::Two Sum C语言
- Leetcode c语言-Two Sum
- LeetCode题解 || Two Sum问题
- 【Leetcode】之Two Sum问题
- LeetCode-问题1-Two Sum
- leetcode之Two Sum问题
- [LeetCode]1 Two Sum(C++,Python实现)
- LeetCode: 1. Two Sum【C 解题】
- leetcode 1:two sum(C语言)
- AngularJS Provider/Service/Factory 使用
- 状态栏修改颜色
- UVa 10908 - Largest Square
- 大数加法算法
- linux 下窗口管理系统
- [c++] LeetCode Two Sum问题
- C++空类默认产生哪些成员函数
- 过滤器类
- 【视频教程】使用UIAutomation开发软件外挂
- java通过JDBC链接SQLServer2012 (亲测有效),并且进行简单的查增删改
- windows 同步机制
- new对象时,类名后加括号与不加括号的区别
- 考前速记
- Textview 和EditView 的提示信息