LeetCode 167 Two Sum II - Input array is sorted
来源:互联网 发布:淘宝商品标题 编辑:程序博客网 时间:2024/06/07 05:35
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
Runtime: 1 ms
public int[] twoSum(int[] numbers, int target) {int i = 0, j = numbers.length - 1;while (i < j) {int sum = numbers[i] + numbers[j];if (sum == target) return new int[]{i + 1, j + 1};else if (sum < target) i++;else j--;}return new int[2];}
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
- 跳台阶
- Android存储--SharedPreferences
- css制作实心、空心arrow箭头
- Java中的代码点和代码单元
- C语言基础--动态内存分配
- LeetCode 167 Two Sum II - Input array is sorted
- Android:两个Activity之间通过Intent传值
- 正则表达式总结
- 二分查找的递归与非递归
- logstash 内置匹配IP
- C/C++ socket编程教程之四:使用socket()函数创建套接字
- 项目2-程序的多文件组织
- 模板
- 注意编码:html代码预览中多出一行””引号,而源码里找不到