【LeetCode】167. Two Sum II - Input array is sorted
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的更多相关文章
- 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 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 ...
- 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 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- LeetCode算法题-Two Sum II - Input array is sorted
这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...
随机推荐
- 架构师成长之路1.1-系统监控工具htop
点击返回架构师成长之路 架构师成长之路1.1-系统监控工具htop htop 是Linux系统中的一个互动的进程查看器,一个文本模式的应用程序(在控制台或者X终端中),需要ncurses. 与Linu ...
- codeforces906 D
题目链接:http://codeforces.com/contest/906/problem/D 题意: 给你n个数,再给你l~r,求%m 题解: 一开始不会 后来查到了欧拉降幂定理: 然后就会了 这 ...
- 【bzoj1492】 NOI2007—货币兑换Cash
http://www.lydsy.com/JudgeOnline/problem.php?id=1492 (题目链接) 题意 两种金券,金券按照比例交易:买入时,将投入的资金购买比例为$rate[i] ...
- js判断是否为空
http://dushanggaolou.iteye.com/blog/1293803 1.<input type="hidden" id="key" n ...
- solr与mysql数据同步的方案
1.使用activeMQ http://blog.csdn.net/zhou2s_101216/article/details/77855413 2.通过配置实现定时同步 http://blog.cs ...
- C# 编码规范、命名规则
1 规范目的 ……………………………………………………… 3 2 适用范围 ……………………………………………………… 3 3 代码注释 ……………………………………………………… 3 3.1 ...
- java基础知识代码-------枚举类型
package com.mon10.day22; /** * 类说明 :枚举类型,案例二 * * @author 作者 : chenyanlong * @version 创建时间:2017年10月22 ...
- python---django中序列化
def get_data(req): ret = {'status':True,'data':None} try: user_list = models.User.objects.all() ret[ ...
- H5 以及 CSS3
<!DOCTYPE html> <html> <head> <style> *{ padding:0; margin:0; } header{ disp ...
- JMS学习(三)ActiveMQ Message Persistence
1,JMS规范支持两种类型的消息传递:persistent and non-persistent.ActiveMQ在支持这两种类型的传递方式时,还支持消息的恢复.中间状态的消息(message are ...