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 ...
随机推荐
- JSTL之forEach的使用详解(简单的技术说得很详细)
在使用JSTL的核心标签库forEach之前,首先需要在JSP中通过taglib指令引入核心标签库: <%@ taglib uri="http://java.sun.com/jsp/j ...
- iOS中 超简单抽屉效果(MMDrawerController)的实现
ios开发中,展示类应用通常要用到抽屉效果,由于项目需要,本人找到一个demo,缩减掉一些不常用的功能,整理出一个较短的实例. 首先需要给工程添加第三方类库 MMDrawerController: 这 ...
- Spark-streaming 连接flume
1,程序为spark的example中的FlumeEventCount示例 object FlumeEventCount { def main(args: Array[String]) { Strea ...
- 03_Weblogic之配置简单域:启动和配置域,使用模板创建域,使用控制台
1 域:概览 是Oracle Weblogic Server的基本管理单元 始终包含一个配置为管理服务器的Oracle WebLogic Server实例 域中可以包括一些称为受管服务器的Ora ...
- Volley请求
1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行H ...
- OC中的类别Category-协议Protocol-…
类别(category)--通过使用类别,我们可以动态地为现有的类添加新方法,而且可以将类定义模块化地分不到多个相关文件中.通常只在类别中定义方法. 类别,接口部分的定义,通常该文件命名为已有&quo ...
- 9.5、Libgdx加速度计
(官网:www.libgdx.cn) 加速度计可以让设备通过三个坐标轴检测加速度.通过加速度可以检测设备的方向. 加速度的单位是米每秒的平方.如果一个坐标轴指向地心,加速度大概是-10米每秒的平方.如 ...
- 解决Plugin is too old,please update to a more recent version,or set ANDROID_DAILY_OVERRIDE..
今天遇到了很诡异的事情. 昨天晚上还好好的工程今天就挂了,提示如下错误: Plugin is too old,please update to a more recent version,or set ...
- Ubuntu快速截图
以前截图,都是按Print键全屏截图,Alt+Print可以截当前的窗口.同时把系统自带的截图工具放到面板上,用的时候点击一下,再选择区域截图,很是不方便.不过,Ubuntu允许自己定义快捷键.要自己 ...
- R-- Dplyr包
Dplyr 包应用 1. 筛选 filter() 按照给定的逻辑判断选择出合适的数据子集 fliter(data,year==2015,month==1) 支持对同一对象的任意条件组合 fliter( ...