【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: TwoSum2
* @Author: xiaof
* @Description: 167. 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.
*
* 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.
*
* @Date: 2019/7/2 10:22
* @Version: 1.0
*/
public class TwoSum2 { public int[] solution(int[] numbers, int target) {
//求两数之和就是对应的target,并且反馈对应的下标
int index1 = 0, index2 = 0;
//这里其实也是遍历,但是已知target,那么我们每次只要获取到第一个数据的下表,然后对后面的数据进行二分查找判断是否有对应的数据就可以了
for(int i = 0; i < numbers.length; ++i) {
index1 = i;
int num1 = numbers[i];
//二分查找目标
int targetNum = target - num1;
int start = i + 1, end = numbers.length - 1;
while(start < end) {
int mid = start + ((end - start) >>> 1);
if(numbers[mid] == targetNum) {
index2 = mid;
break;
} else if (numbers[mid] > targetNum) {
//如果目标位置比目标数据大,说明要找的数据在前面
end = mid;
} else {
//如果比目标数据小,说明再后面,然后我们mid的位置已经做了比较,所以后移一位
//因为mid = start + (end - start) >>> 1; 可能正好等于start
start = mid + 1;
}
} if(start == end) {
//如果首尾相等,那么手动判断一下
if(targetNum == numbers[start]) {
index2 = start;
}
} if(index2 != 0) {
break;
}
} int result[] = new int[2];
result[0] = index1 + 1;
result[1] = index2 + 1;
return result;
} //网上大牛最快,也是占内存最小的算法,有点类似快排的思想
public int[] twoSum(int[] numbers, int target) {
int []res = new int [2];
int index1 = 0;
int index2 = numbers.length - 1;
while (index2 > index1){
if (numbers[index1] + numbers[index2] > target){
index2 --;
}
else if(numbers[index1] + numbers[index2] < target){
index1 ++;
}else{
res[0] = index1+1;
res[1] = index2+1;
break;
}
}
return res;
} public static void main(String args[]) { int pres[] = {5,25,75};
int target = 100;
System.out.println(new TwoSum2().solution(pres, target)); } }
【LEETCODE】38、167题,Two Sum II - Input array is sorted的更多相关文章
- LeetCode算法题-Two Sum II - Input array is sorted
这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...
- 【Leetcode 167】Two Sum II - Input array is sorted
问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数.返回这两个数的下标(从1开始),其中第1个下标比第2个下标小. Input: numbers={2, 7, 11, 15}, t ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 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【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@python
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- LeetCode_167. Two Sum II - Input array is sorted
167. Two Sum II - Input array is sorted Easy Given an array of integers that is already sorted in as ...
- 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], ...
随机推荐
- 开源项目 01 HtmlAgilityPack
using HtmlAgilityPack; using System; using System.Collections.Generic; using System.Linq; using Syst ...
- P2210 Haywire
P2210 Haywire 模拟退火练手题 #include<cmath> #include<ctime> #include<cstdio> #include< ...
- 干货 | 10分钟掌握branch and cut(分支剪界)算法原理附带C++求解TSP问题代码
00 前言 branch and cut其实还是和branch and bound脱离不了干系的.所以,在开始本节的学习之前,请大家还是要务必掌握branch and bound算法的原理. 01 应 ...
- 和jz姐姐的vp记录
即使如此,jz姐姐也漂亮的取得了胜利 有些懒得写直接口胡,所以代码也不一定有 暂时停更了 2015-2016 Petrozavodsk Winter Training Camp, Makoto rng ...
- UOJ46. 【清华集训2014】玄学 [线段树,二进制分组]
UOJ 思路 模拟赛出了这题,结果我没学过二进制分组--一波主席树然后空间就爆炸了-- 用线段树维护时间序列,每个节点维护\(a_i\to x_i\times a_i+b_i,i\in [1,n]\) ...
- js constructor typeOf 区别
constructor 属性返回对创建此对象的数组函数的引用. 例如:const obj = {a: 1} console.log(obj.constructor) // funct ...
- mysql 获取星期几,dayofweek()函数
mysql> ; +------------------------+ | dayofweek(curdate())- | +------------------------+ | | +--- ...
- Centos分区/超过2T的磁盘
centos分区大于2TB 用parted分区工具分区 fdisk -l 查看要分的区(我这里是/dev/vdb) parted /dev/vdb #进入/dev/vdb进行分区 mktabl ...
- Win10电脑桌面壁纸自动变成黑色无法更换怎么解决
很多用户在升级到win10之后,发现在使用过程中经常会碰到一些问题,就是电脑桌面壁纸总是会自动变成黑色,而且无法设置桌面背景壁纸,这是怎么回事呢,出现这样的问题可能是因为系统不是正版,或者是电脑设置不 ...
- Spring MVC JSON乱码问题
之前项目中也遇到过返回JSON时乱码问题,当时找到了一个方法解决了问题但是没有明白原因,今天这个项目又遇到了JSON乱码问题,用之前的方法不行,看了这篇博文才明白为什么 @RequestMapping ...