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 ...
随机推荐
- hive编程指南——读书笔记(无知拾遗)
set hive.metastore.warehouse.dir=/user/myname/hive/warehouse; 用户设定自己的数据仓库目录.不影响其他用户.也在$HOME/.hiverc中 ...
- 【Unity Shaders】使用Unity Render Textures实现画面特效——建立画面特效脚本系统
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- SDL2源代码分析1:初始化(SDL_Init())
===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...
- JAVA中的静态成员
//Java中的静态成员 /* *静态的成员变量是属于类的,不属于某个对象,是共享的. * 访问时可以用类名.静态属性直接访问,也可以用对象.访问,后者不提倡. * 静态的成员方法只能访问静态的成员 ...
- DB 查询分析器 方便地创建DB2自定义函数
DB 查询分析器 方便地创建DB2自定义函数 马根峰 (广东联合电子服务股份有限公司, 广州 510300) 摘要 ...
- Android启动过程分析
Android系统启动过程 首先看一张Android框架结构图 Linux内核启动之后就到Android Init进程,进而启动Android相关的服务和应用. 启动的过程如下图所示:(图片来自网上, ...
- MinerStoreThread.java 存储线程
MinerStoreThread.java 存储线程 package com.iteye.injavawetrust.miner; import java.util.HashMap; import j ...
- 【一天一道LeetCode】#30. Substring with Concatenation of All Words
注:这道题之前跳过了,现在补回来 一天一道LeetCode系列 (一)题目 You are given a string, s, and a list of words, words, that ar ...
- 简译《Dissecting SQL Server Execution Plans》——连载总入口
转载请注明出处 由于工作及学习需要,最近看了一下<Dissecting SQL Server Execution Plans>,这是少有的专门描述执行计划的优秀书籍,为了快速查找并供入门同 ...
- Ext JS 5初探(三)
在上文提到了本地化文件的问题,然后在Ext JS 5的包里找了找,居然还没包含本地化包.我估计目前还不到考虑本地化的时候.在Sencha Touch中,是没有本地化包的,但是要让Ext JS也不包含本 ...