博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【LeetCode】167. Two Sum II - Input array is sorted
阅读量:5099 次
发布时间:2019-06-13

本文共 1171 字,大约阅读时间需要 3 分钟。

Difficulty:easy

 More:

Description

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 = 9Output: [1,2]Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

Intuition

refer to 

 

Solution

public int[] twoSum(int[] numbers, int target) {        if(numbers==null || numbers.length<=1)            throw new IllegalArgumentException("invalid array!");        int low=0;        int high=numbers.length-1;        while(low
target){ high--; } } throw new IllegalArgumentException("no such sum"); }

  

Complexity

Time complexity : O(n)

Space complexity : O(1)

 

What I've learned

1.The use of two pointers.

 

 More:

 

转载于:https://www.cnblogs.com/yongh/p/9999039.html

你可能感兴趣的文章
css3之transform-origin
查看>>
[转]JavaScript快速检测浏览器对CSS3特性的支持
查看>>
Master选举原理
查看>>
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>
小别离
查看>>
微信小程序-发起 HTTPS 请求
查看>>
WPF动画设置1(转)
查看>>
backgound-attachment属性学习
查看>>
个人作业——关于K米的产品案例分析
查看>>
基于node/mongo的App Docker化测试环境搭建
查看>>
java web 中base64传输的坑
查看>>
java 中的线程(一)
查看>>
秒杀9种排序算法(JavaScript版)
查看>>
素数判断BFS之“Prime Path”
查看>>
Activiti入门 -- 环境搭建和核心API简介
查看>>
struts.convention.classes.reload配置为true,tomcat启动报错
查看>>
MySQL的并行复制多线程复制MTS(Multi-Threaded Slaves)
查看>>
Django中间件
查看>>
xcode 5.1安装vvdocument
查看>>
好玩的-记最近玩的几个经典ipad ios游戏
查看>>