[LeetCode]Two Sum
来源:互联网 发布:excel圆环图数据标志 编辑:程序博客网 时间:2024/06/06 16:46
题目要求: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
这道题其实不难,剑指offer里也有一道相似的,即面试题41(和为S的两个数字),解题思路一模一样,即:
首先默认这个数组是排好序的,那么我们可以用两个指针i和j,分别指向数组首部和数组尾部。设和s=a[i] + a[j],如果s<target,
说明需要更大的数相加,因为j已经是最大的数了,因此只能将i向后移一位,找一个大一点的数与j指向的数相加。
同理,若s>target,说明当前j指向的数太大了,我们需要将j向前移动,找一个小一点的数与当前i指向的数相加,
继续测试直到找到能使和s==target的两个数为止。
本题首先要注意的是,数组不是预先排序的,所以需要自己先对数组排序。但这会产生一个问题,即我们排好序后,
原来数字所在的位置就不是原数组中的位置了,因此需要一个辅助的数组结构来存储数据在原数组中的位置。
另外,在产生结果的时候,也要注意输出的两个位置是由小到大输出,因此也需要做一些处理工作。
以下是我写的代码,欢迎各位大牛指导交流:
AC,Runtime: 12 ms
//LeetCode_Two Sum//Written by zhou//2013.11.1//辅助数据结构struct node{int value;int pos;};class Solution {public: vector<int> twoSum(vector<int> &numbers, int target) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int i = 0, j = numbers.size() - 1; //将原数组值和位置复制到nodeArray数组中node *nodeArray = new node[numbers.size()];for (int k = 0; k < numbers.size(); ++k){ nodeArray[k].value = numbers[k]; nodeArray[k].pos = k + 1;} //对nodeArray中进行快排操作 QuickSort(nodeArray,i,j); //寻找解 while( i < j) { int temp = nodeArray[i].value + nodeArray[j].value; if (temp == target) { vector<int> res; //这里注意输出解的位置是由小到大 int posA = nodeArray[i].pos; int posB = nodeArray[j].pos; if (posA > posB) { posA = posA ^ posB; posB = posA ^ posB; posA = posA ^ posB; } res.push_back(posA); res.push_back(posB); delete[] nodeArray; return res; } else if (temp < target) { ++i; } else --j; } } //交换两个node类型的数据的值 void Swap(node &a, node &b) {int temp = a.value;a.value = b.value;b.value = temp;temp = a.pos;a.pos = b.pos;b.pos = temp; } //快排实现 void QuickSort(node *numbers, int left, int right) { int i = left, j = right + 1;int pivot = numbers[left].value; while(i < j) {while(++i <= right && numbers[i].value < pivot){}while(--j >= left && numbers[j].value > pivot){} if (i < j) { Swap(numbers[i],numbers[j]); } } Swap(numbers[left],numbers[j]); if (left < j-1) QuickSort(numbers, left, j-1); if (j+1 < right) QuickSort(numbers, j+1, right); }};
- 【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
- LeetCode | Two Sum
- 装扮装扮dijit.Tree
- PHP实现MVC开发: 一个简单的MVC
- Ruby开发入门
- 用OPenGL实现 Bresenham画线算法
- Qt模块化笔记之sql——执行sql语句
- [LeetCode]Two Sum
- 解决win和ubuntu12.10下adb devices 无法检测android手机问题
- poj 3694 Network
- 用OpenGL实现 中点画圆法
- 斐波纳契数列
- preloader架构及流程图
- 计算机视觉、机器学习相关领域论文和源代码大集合--持续更新……
- (原)Win8 关机批处理脚本
- 关于今天老师讲的树的一些基本练习