LeetCode-TwoSum
来源:互联网 发布:mac的路径怎么写 编辑:程序博客网 时间:2024/05/23 19:15
作者:disappearedgod
文章出处:http://blog.csdn.net/disappearedgod/article/details/23634067
时间:2014-4-13
题目
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
思路:
1.克隆排序
2.双指针查找
3.对应返回位置
C++ 版本
class Solution {public: vector<int> twoSum(vector<int> &numbers, int target) { vector<int> num = numbers; std::sort(num.begin(),num.end()); int length = num.size(); int left = 0; int right = length -1; int sum = 0; vector<int> index; while(left<right){ sum = num[left]+num[right]; if(sum > target) --right; else if(sum < target) ++left; else{ for(int i = 0; i < length ; ++i){ if(index.size()==2) break; if(numbers[i]==num[left]) index.push_back(i+1); else if(numbers[i]==num[right]) index.push_back(i+1); } return index; } } }};
java 版本-有问题
public class Solution { public int[] twoSum(int[] numbers, int target) { int[] num = numbers.clone(); Arrays.sort(num);//unstability sort int length = numbers.length; int left = 0; int right = length -1; int sum = 0; int[] index = new int[2]; while(left<right){ sum= num[left] + num[right]; if(sum<target) ++left; else if(sum > target) --right; else{ int count=0; for(int i = 0; i < length; i++){ if(count==2) break; if(numbers[i]==num[left]){ index[0]=i+1; ++count; continue; } else if(numbers[i]==num[right]){ index[1]=i+1; ++count; continue; } } break; } } return index; }}
问题在于
if(numbers[i]==num[left]){
如果两个因子相同,则只改变了left,而没有改变right,所以要用容器
public class Solution { public int[] twoSum(int[] numbers, int target) { int[] num = numbers.clone(); Arrays.sort(num);//unstability sort int length = numbers.length; int left = 0; int right = length -1; int sum = 0; int[] index = new int[2]; ArrayList<Integer> list = new ArrayList<Integer>();// not int while(left<right){ sum= num[left] + num[right]; if(sum<target) ++left; else if(sum > target) --right; else{ list.clear(); for(int i = 0; i < length; i++){ if(list.size()==2) break; if(numbers[i]==num[left]){ list.add(i+1); } else if(numbers[i]==num[right]){ list.add(i+1); } } break; } } int i = 0; for(Iterator it = list.iterator();it.hasNext();++i){ index[i]=(int)it.next(); } return index; }}
借鉴:http://blog.csdn.net/lilong_dream/article/details/19298357
返回
LeetCode Solution(持续更新,java>c++)
0 0
- [leetcode]-twoSum
- [leetcode]twoSum
- LeetCode-TwoSum
- 【Leetcode】TwoSum
- LeetCode - TwoSum
- leetcode :twoSum
- leetcode TwoSum
- leetcode twosum
- leetcode--twoSum
- LeetCode twoSum
- LeetCode-TwoSum
- leetcode--twoSum
- leetcode TwoSum
- TwoSum leetcode
- leetcode:twosum
- leetcode:TwoSum
- LeetCode---TwoSum
- LeetCode-TwoSum
- 亚伦•斯沃茨:提升时间的品质
- jsoup+jxl java代码post请求,并解析返回的html
- cocos2dx2.2 自适应屏幕
- 指针交换三个数 排序输出
- hdu1108 GCD LCM 模板
- LeetCode-TwoSum
- 这些代码,我怎么看都不觉得厌烦!
- 配置Tomcat成为系统服务
- 黑马程序员—Java泛型
- 想要提高记忆力?做梦去吧!
- register_chrdev_region注册
- JavaScript事件委托的技术原理
- 真想学习?扔掉你详细的计划吧!
- scanf("%d%d",&x,&y);与scanf("%d,%d",&x,&y);的区别 c语言输入注意