leetcode :Binary Search:Two Sum II - Input array is sorted(069)
来源:互联网 发布:个人考勤软件 编辑:程序博客网 时间:2024/05/29 04:20
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
–
http://www.cnblogs.com/grandyang/p/5185815.html
// O(n)class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { int l = 0, r = numbers.size() - 1; while (l < r) { int sum = numbers[l] + numbers[r]; if (sum == target) return {l + 1, r + 1}; else if (sum < target) ++l; else --r; } return {}; }};
0 0
- leetcode :Binary Search:Two Sum II - Input array is sorted(069)
- leetcode 167: Two Sum II - Input array is sorted
- Leetcode NO.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
- 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 (Java)
- [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 C++
- [leetcode]167. Two Sum II - Input array is sorted
- The ultimate SO_LINGER page, or: why is my tcp not reliable
- 使用JSON在前台与后台之间进行简单的传值
- oleDBConn.ServerVersion 未注册"Microsoft.ACE.OLEDB.12.0"提供程序 或oleDBConn(0x80004005)或oleDBConn 未知错误
- 【方法】方法重载与方法重写
- 什么是Hive
- leetcode :Binary Search:Two Sum II - Input array is sorted(069)
- 《集体智慧编程》笔记2.1---第二章
- 98-7
- 使用LinearLayout+TextView实现底部导航栏,重复切换页面不会重新加载
- 【BZOJ】3016: [Usaco2012 Nov]Clumsy Cows
- Java transient关键字使用小记
- leetcode :Binary Search:Dungeon Game(174)
- 《数学之美》读书笔记3
- 大话直播,做直播看我就够了