[leetcode]167. Two Sum II - Input array is sorted
来源:互联网 发布:java spring框架详解 编辑:程序博客网 时间:2024/05/17 04:56
解法一:没有用到已排序属性
class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { map<int,int>m; map<int,int>::iterator iter; vector<int>ret; for(int i=0;i<numbers.size();i++) { iter=m.find(target-numbers[i]); if(iter!=m.end()) { ret.push_back(iter->second+1); ret.push_back(i+1); } m.insert(pair<int,int>(numbers[i],i)); } return ret; }};
解法二
class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { vector<int>ret; int l=0; int r=numbers.size()-1; while(l<r) { if(numbers[l]+numbers[r]==target) { ret.push_back(l+1); ret.push_back(r+1); return ret; } if(numbers[l]+numbers[r]>target) { r--; } else { l++; } } return ret; }};
0 0
- Leetcode-167.Two Sum II - Input array is sorted
- [leetcode] 167. Two Sum II - Input array is sorted
- 【leetcode】167. Two Sum II - Input array is sorted
- 【LeetCode】167. Two Sum II - Input array is sorted
- [LeetCode OJ]167. Two Sum II - Input array is sorted
- [leetcode]167. Two Sum II - Input array is sorted
- leetcode-java-167. Two Sum II - Input array is sorted
- 【leetcode】167. Two Sum II - Input array is sorted
- LeetCode—167. Two Sum II - Input array is sorted
- [LeetCode]167. Two Sum II - Input array is sorted
- [LeetCode] 167. Two Sum II - Input array is sorted
- [leetcode] 167. Two Sum II - Input array is sorted
- LeetCode 167. Two Sum II - Input array is sorted
- LeetCode 167. Two Sum II - Input array is sorted
- leetcode ----167. Two Sum II - Input array is sorted
- leetcode 167. Two Sum II - Input array is sorted
- LeetCode 167. Two Sum II - Input array is sorted
- [Leetcode]167. Two Sum II - Input array is sorted
- json格式数据
- dlib编译出错以及解决方法
- [PAT] B1013
- 剑指offer(41-50)
- 欢迎使用CSDN-markdown编辑器
- [leetcode]167. Two Sum II - Input array is sorted
- python opencv 图像按位操作
- java并发你必须会的编程
- 1029. Median (25)
- 《JQuery基础教程》第四版课后练习代码--第六章
- 精确记算程序的运行时间或者某段代码的运行时间
- Jenkins在windows上安装
- android realm数据库基本使用
- Efficient Gradient-domain Compositing