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:

  1. Input: numbers = [2,7,11,15], target = 9
  2. Output: [1,2]
  3. Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
  1. package leetcode.easy;
  2.  
  3. public class TwoSumIIInputArrayIsSorted {
  4. @org.junit.Test
  5. public void test() {
  6. int[] numbers = { 2, 7, 11, 15 };
  7. int target = 9;
  8. System.out.println(twoSum(numbers, target));
  9. }
  10.  
  11. public int[] twoSum(int[] numbers, int target) {
  12. for (int i = 0; i < numbers.length - 1; i++) {
  13. for (int j = i + 1; j < numbers.length; j++) {
  14. if (numbers[i] + numbers[j] == target) {
  15. return new int[] { i + 1, j + 1 };
  16. }
  17. }
  18. }
  19. return new int[] { 0, 0 };
  20. }
  21. }

LeetCode_167. Two Sum II - Input array is sorted的更多相关文章

  1. leetcode2 Two Sum II – Input array is sorted

    Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...

  2. 29. leetcode 167. Two Sum II - Input array is sorted

    167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...

  3. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  4. 167. Two Sum II - Input array is sorted@python

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  5. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  6. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  7. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  8. [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  9. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

随机推荐

  1. 大数据之路week06--day07(完全分布式Hadoop的搭建)

    前提工作: 克隆2台虚拟机完成后:新的2台虚拟机,请务必依次修改3台虚拟机的ip地址和主机名称[建议三台主机名称依次叫做:master.node1.node2 ] 上一篇博客 (三台虚拟机都要开机) ...

  2. certbot更新错误

    自动更新老是提示这个错误. root@vultr:~/certbot# ./certbot-auto Upgrading certbot-auto 0.29.1 to 0.34.2... Couldn ...

  3. 前端学习笔记--Visual Studio Code安装及中文显示

    1.在官网https://code.visualstudio.com/下载对应的版本: 2.安装 一路点击下一步,选中  添加到PATH后,安装. 安装成功,可以直接打开使用: 把界面改成中文显示: ...

  4. 前端学习笔记--CSS样式--列表和表格

    1.列表 2.表格 odd:奇数  even:偶数

  5. 前端处理:elementUI 表格索引代表第几条数据

    分析:表格结合分页 知识点:1.表格的自定义索引(索引以当前行的行号作为参数)number, Function(index)该属性传入数字时,将作为索引的起始值.也可以传入一个方法,它提供当前行的行号 ...

  6. rownum行号

    1.rownum是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,且rownum不能以任何表的名称作为前缀. 如: ...

  7. Mac---使用tree生成目录结构

    作为测试或者程序员,平时都有需求来生成目录树,进行项目层级的讲解等.如下是样式图: 实现方式,即操作步骤: Mac下使用 brew install tree 进行安装,安装后,在terminal中输入 ...

  8. Mysql-Percona mysql5.7简单安装

    Mysql-Percona mysql5.7简单安装 一.什么是Percona 单从mysql的角度来讲,可以把Percona理解为mysql的一个分支,因为mysql的源码是开源的,Percona就 ...

  9. 树莓派远程桌面--使用windows自带的远程桌面软件mstsc.exe

    步骤: 1.使用SSH登录树莓派 2.安装xrdp服务: sudo apt-get install xrdp 3.在Windows下打开命令框运行:mstsc 4.登录树莓派桌面 5.登录成功

  10. 使用JSP/Servalet技术开发新闻发布系统------JSP数据交互一

    什么是内置对象 JSP内置对象是 Web 容器创建的一组对象,不用通过手动new就可以使用 JSP中的九大内存对象  request 请求对象  response 响应对象  out   输出对象  ...