Two Sum II - Input array is sorted
来源:互联网 发布:php博客管理系统源码 编辑:程序博客网 时间:2024/05/22 09:00
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 int[] twoSum(int[] numbers, int target) { int[] res = { 0, 0 };if (numbers == null || numbers.length < 1)return res;int i = 0, j = numbers.length - 1;while(i < j){int sum = numbers[i] + numbers[j];if (sum < target) {i++;} else if (sum > target) {j--;} else {res[0] = i + 1;res[1] = j + 1;break;}}return res;}
0 0
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Two Sum II - Input array is sorted
- Leetcode167 - Two Sum II - Input array is sorted(two pointers)
- 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
- 167 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
- [LeetCode167]Two Sum II - Input array is sorted
- 服务器/电脑蓝屏,错误代码:0X0000007B 的解决办法
- 转 Java Classloader机制解析
- 基于用户的协同过滤算法的电影推荐系统
- Sikuli 结合selenium的一些使用
- HihoCoder]#1361 : Playfair密码表
- Two Sum II - Input array is sorted
- ELK之Kibana
- Android获取验证码后倒计时
- HTML笔记(一)
- 华为OJ 公共字串计算&&查找两个字符串a,b中的最长公共子串
- python学习(9)———文件操作
- javaee之Struts2辅助功能
- CF刷题总结---CF706E链表
- 利用javadoc定制自己的接口文档(二)