167. Two Sum II - Input array is sorted

Easy

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.

Note:

  • Your returned answers (both index1 and index2) are not zero-based.
  • You may assume that each input would have exactly one solution and you may not use the same element twice.

Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
package leetcode.easy;

public class TwoSumIIInputArrayIsSorted {
@org.junit.Test
public void test() {
int[] numbers = { 2, 7, 11, 15 };
int target = 9;
System.out.println(twoSum(numbers, target));
} public int[] twoSum(int[] numbers, int target) {
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] == target) {
return new int[] { i + 1, j + 1 };
}
}
}
return new int[] { 0, 0 };
}
}

最新文章

  1. Oracle数据库备份、恢复及常见问题
  2. iOS版本更新的App提交审核发布流程
  3. 微软Face API体验——人脸检测
  4. app 支付宝 支付 alipaySdk
  5. PHP数组处理函数的使用array_push(一)
  6. HDU 5762
  7. KVC笔记
  8. Java_获取当前月最后一天
  9. Sql Server 常用方法、存储过程备用
  10. jstl标签库基础教程及其使用代码(一)。
  11. 为什么在我眼里你是一只傻逼——傻逼“常所用”句型之(2)——“当当网的就有XXX人评论,YYY%的推荐”
  12. oracle 生成随机数【待整理】
  13. python操作redis-过期时间
  14. JavaScript检测原始值、引用值、属性
  15. 树后台数据存储(採用webmethod)
  16. A Game of Thrones(19) - Jon
  17. Webserver管理系列:3、Windows Update
  18. hdu 2197 求长度为n的本原串 (快速幂+map)
  19. FreeCodeCamp----Intermediate Algorithm Scripting解法
  20. (转) argparse — 解析命令参数和选项

热门文章

  1. 【转】angular使用代理解决跨域
  2. (一)python3.7的安装
  3. 聊聊Hash索引
  4. &quot;==&quot;和equals小结
  5. 【bzoj3238】差异 后缀树
  6. jquery手机端产品列表响应式宽高检测宽度赋值给高度让宽高相同
  7. Codeforces Round #597 (Div. 2) B. Restricted RPS
  8. jQuery相关方法9----事件相关
  9. TensorFlow(一):准备
  10. hadoop笔记-hdfs文件读写