LeetCode[167] Two Sum II - Input array is sorted
来源:互联网 发布:java 判断类型相同 编辑:程序博客网 时间:2024/06/07 01:21
Given an array of integers that is already sorted in ascending order, 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
class Solution {public:vector<int> twoSum(vector<int>& numbers, int target) {vector<int> res;int it1 = 0, it2 = numbers.size() - 1;while (it1 < it2) {if (numbers[it1] + numbers[it2] == target) {res.push_back(it1 + 1);res.push_back(it2 + 1);break;}else if (numbers[it1] + numbers[it2] < target){++it1;}else {--it2;}}return res;}};
0 0
- leetcode 167: Two Sum II - Input array is sorted
- Leetcode NO.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 C++
- 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-Two Sum II - Input array is sorted
- [Leetcode]Two Sum II - Input array is sorted
- leetcode--Two Sum II - Input array is sorted
- LeetCode 题解(274) : Two Sum II - Input array is sorted
- [LeetCode] Two Sum II – Input array is sorted
- ViewPager实现广告轮播---(一)(无限循环)
- 《Spring揭秘》第二章 读书笔记 —— IoC的基本概念
- C++11中值得关注的几大变化
- java内联函数
- 服务治理(转载)
- LeetCode[167] Two Sum II - Input array is sorted
- 知道这几个正则表达式能让你少写 1,000 行代码
- git提交空文件夹
- 页面弹框的一个小插件(可结合Ztree使用)
- 初识JSON
- POJ 1228
- Android入门篇---01:搭建Android开发环境
- 【Unity】Undo/撤销
- 信息安全技术实验二网络扫描实验+ARP攻击