Difficulty:easy

 More:【目录】LeetCode Java实现

Description

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/

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 exactlyone solution and you may not use the sameelement 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.

Intuition

refer to 和为s的两个数字

Solution

    public int[] twoSum(int[] num, int target) {
int i=0, j=num.length-1;
while(i<j){
if(num[i]+num[j]<target)
i++;
else if(num[i]+num[j]>target)
j--;
else
return new int[]{i+1,j+1};
}
return null;
}

  

Complexity

Time complexity : O(n)

Space complexity : O(1)

What I've learned

1.The use of two pointers.

 More:【目录】LeetCode Java实现

【LeetCode】167. Two Sum II - Input array is sorted的更多相关文章

  1. 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  2. Leetcode No.167 Two Sum II - Input array is sorted(c++实现)

    1. 题目 1.1 英文题目 Given an array of integers numbers that is already sorted in non-decreasing order, fi ...

  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 - LeetCode

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

  5. 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 ...

  6. 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 ...

  7. 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 ...

  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算法题-Two Sum II - Input array is sorted

    这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...

随机推荐

  1. BZOJ3738 [Ontak2013]Kapitał 【扩展Lucas】

    题目链接 BZOJ3738 题解 复习 同上 但是为了消去因子\(10\),处理\(2^k\)的时候,乘回\(2^{k_1}\)时,应同时计算\(5^{k_2}\) 如果\(k_1 \ge k_2\) ...

  2. java 7 升级后,控制面板里找不到java图标了

    如果电脑里只装了jre的情况下,好像从java 7 update 9开始,通过java自动升级程序完成升级后,控制面板里的java图标就不见了. 只好重新从java.sun.com上下载最新版的jre ...

  3. 详解掩膜mask

    什么是掩膜(mask) 数字图像处理中的掩膜的概念是借鉴于PCB制版的过程,在半导体制造中,许多芯片工艺步骤采用光刻技术,用于这些步骤的图形“底片”称为掩膜(也称作“掩模”),其作用是:在硅片上选定的 ...

  4. JAVA (字符串学习)

    String类 String类是不可改变的,所以你一旦创建了String对象,那它的值就无法改变了. 如果需要对字符串做很多修改,那么应该选择使用StringBuffer&StringBuil ...

  5. mongodb的认证(authentication)与授权(authorization)

    一小白瞎整mongodb,认证部分被折磨的惨不忍睹,看厮可怜,特查了一下文档,浅显地总结一下mongodb认证(authentication)与授权(authorization)的联系. 创建的所有用 ...

  6. JavaScript编写风格指南 (二)

    七:注释 // 频繁的使用注释有助于他人理解你的代码// 1.代码晦涩难懂// 2.可能被误认为是错误的代码// 3.必要但不明显的针对特定浏览器的代码// 4.对于对象,方法或者属性,生成文档是有必 ...

  7. es6笔记(5)Map数据结构

    概要 字典是用来存储不重复key的Hash结构.不同于集合(Set)的一点,字典使用的是[key,value]的形式来存储数据. JavaScript的对象(Object:{})只能用字符串当做key ...

  8. 进程ID[PID(Process ID)]与端口号[(Port ID)]的联系

    1.首先声明一点:PID不是端口(port id),而是Process ID进程号的意思. 2.那么,什么是进程号? 采集网友的意见就是: 进程号,是系统分配给么一个进程的唯一标识符.PID就是各进程 ...

  9. 第5月第27天 cocos2d

    1. 流程是这样的: 在CCApplication的run函数中,显示设备链调用相应的场景显示函数drawScene来绘制场景,然后调用了CCScheduler的update函数,在这个函数里,对所有 ...

  10. 定义和用法 DATEDIFF() 函数返回两个日期之间的天数

    SELECT DATEDIFF(YEAR,'1986-09-01','2016-02-01') AS DiffDate datepart 缩写 年 yy, yyyy 季度 qq, q 月 mm, m ...