two sum II
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. Please note that 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.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2 返回的是下标加1
这里已经是有序的数组,所以不需要像无序一样使用hashmap了。这里直接可以从两端向中间移。见代码
class Solution {
public int[] twoSum(int[] numbers, int target) {
if(numbers==null||numbers.length==0) return null;
int[] re=new int[2];
//从前后一起向中间遍历,因为有序,可以这样做
for(int i=0,j=numbers.length-1;i<j;){
if(numbers[i]+numbers[j]==target){
re[0]=i+1;
re[1]=j+1;
return re;
}else if(numbers[i]+numbers[j]>target){
j--; //因为有序,当两者相加大于target,说明需要减小,右边的左移
}else{
i++;
}
}
return re;
}
}
当然,因为有序,也可以使用二分查找。从头开始遍历每个元素,在该元素右边的数组中二分查找target-该元素,没有就遍历下一个元素。二分查找不用管该元素的左边元素,因为是从左边开始遍历的,在右边找不到对应的,所以遍历到右边时,左边肯定是没有对应元素的。
见代码,这是c++代码,原理是一样的,看关键步骤。。这个运行时间大于上面的代码,这里主要掌握思路,也是一种解法
vector<int> twoSum(vector<int> &numbers, int target) {
if(numbers.empty()) return {};
for(int i=0; i<numbers.size()-1; i++) {//遍历所有元素
int start=i+1, end=numbers.size()-1, gap=target-numbers[i];
while(start <= end) { //在右边的元素中使用二分查找与该元素对应的元素。
int m = start+(end-start)/2;
if(numbers[m] == gap) return {i+1,m+1};
else if(numbers[m] > gap) end=m-1;
else start=m+1;
}
}
}
two sum II的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【leetcode】Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 32. Path Sum && Path Sum II
Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- 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], ...
- hdoj 1977 Consecutive sum II
Consecutive sum II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- React Native自动化测试
大凡做软件开发,肯定会涉及到很多的测试,本地测试,Junit测试,用例测试等,今天就来说说RN的测试. React Native的官方代码仓库里有一些测试代码,你可以在贡献代码之后回归测试一下,以检测 ...
- Java中封装性的使用
//Java面对对象基本特性之一:封装性 //作用:保护某些属性和方法不被外部所看见 //封装的实现:通过关键字private声明 //鼠标右键--->Source---->Generat ...
- Android官方命令深入分析之hprof-conv
hprof-conv工具可以将Android SDK工具生成的HPROF文件生成一个标准的格式,这样你就可以使用工具进行查看: hprof-conv [-z] <infile> <o ...
- 【Unity Shaders】Reflecting Your World —— Unity3D中的法线贴图和反射
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- javascript之DOM编程正则表达式引入
在javascript中,正则表达式和java中区别不大.只有一小部分不同的地方: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...
- JS开发引用HTML DOM的location和document对象
上一次提到,在报表软件FineReport的JavaScript开发中,可以访问并处理的HTML DOM对象有windows.location.document三种.这次就继续介绍后两种,locati ...
- 【Android 应用开发】Android 网络编程 API笔记 - java.net 包相关 接口 api
Android 网络编程相关的包 : 9 包, 20 接口, 103 类, 6 枚举, 14异常; -- Java包 : java.net 包 (6接口, 34类, 2枚举, 12异常); -- An ...
- (NO.00001)iOS游戏SpeedBoy Lite成形记(十八)
现在需要实现具体的游戏逻辑大致如下: 玩家点击某条赛道选择一个选手,然后会弹出菜单窗口让玩家输入压赌的金额,如果输入的金额值非法,则在GameInterface下部的状态栏中显示提示,要求玩家重新输入 ...
- mysql进阶(二)索引简易教程
Mysql索引简易教程 基本概念 索引是指把你设置为索引的字段A的内容储存在一个独立区间S里,里面只有这个字段的内容.在找查这个与这个字段A的内容时会直接从这个独立区间里查找,而不是去到数据表里查找. ...
- JAVA之旅(六)——单例设计模式,继承extends,聚集关系,子父类变量关系,super,覆盖
JAVA之旅(六)--单例设计模式,继承extends,聚集关系,子父类变量关系,super,覆盖 java也越来越深入了,大家加油吧!咱们一步步来 一.单例设计模式 什么是设计模式? JAVA当中有 ...