【LeetCode】 167. Two Sum II - Input array is sorted
来源:互联网 发布:破解windows开机密码 编辑:程序博客网 时间:2024/09/21 08:15
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
public class Solution { public int[] twoSum(int[] numbers, int target) { int left = 0, right = numbers.length - 1; int[] res = new int[2]; while (left < right) { if (numbers[left] + numbers[right] < target) { left++; } else if (numbers[left] + numbers[right] > target) { right--; } else { res[0] = left + 1; res[1] = right + 1; break; } } return res; }}
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
- 数组,vector,map,list,queue区别
- CoreData应用UI活动指示器不显示的解决
- P1141 01迷宫
- Leetcode_005_Longest_Palindromic_Substring
- 《DSAA》 12.1 自顶向下伸展树
- 【LeetCode】 167. Two Sum II - Input array is sorted
- 跨域请求
- 389. Find the Difference
- 查看端口被占用
- Oracle数据库之数据处理
- go 语言 TCP服务器接受客户端字符串信息
- Pycharm的运行和简单调试
- 每天一个 Linux 命令(23):Linux 目录结构
- Delphi日期函数大全