[leetcode] 167. Two Sum II - Input array is sorted 解题报告
来源:互联网 发布:win10家庭版装c语言 编辑:程序博客网 时间:2024/05/22 09:38
题目链接:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
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
思路: 简单的two points.
代码如下:
class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { int len=numbers.size(), left = 0, right = len-1; while(left < right) { int val = numbers[left] + numbers[right]; if(val == target) return vector<int>{left+1, right+1}; else if(val > target) right--; else left++; } return {}; }};
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】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
- bzoj1009[GT考试] dp+矩阵快速幂
- Java_intellij idea 15 + tomcat + servlet 配置教程
- HDU 2105 The Center of Gravity (水题)
- 《数据结构》顺序栈
- oracle GROUPING函数
- [leetcode] 167. Two Sum II - Input array is sorted 解题报告
- 前端性能优化--能用css的地方尽量不要用js
- html链接和框架标签
- 部分JAVA设计模式在Android中的体现。
- 互联网面试总结(二) : 概述题
- ATEN宏正盛装出席Infocomm China 2016
- 互联网面试总结(三) : 算法
- 4200: [Noi2015]小园丁与老司机 DP+有源汇上下界最小流
- bzoj4300 绝世好题 动态规划