【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], ...
随机推荐
- art-template模板引擎高级使用
一.结合express的基本使用 // npm下载express/art-template/express-art-tempalte,并且加载 var express=require('express ...
- linux系统管理——账号权限及归属管理练习
1.创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) 2.添加组帐号zonghe.caiwu.jishu,GID号分别设置为2001.2002.200 ...
- c博客作业—分支,结构顺序
1展现PTA总分 1 2 2本章学习类容总结 1常量和变量 常量:在运行中其值不变的量被称为常量,常量的类型通常是由书写格式决定,包括整型常量,实数型变量等等. 变量: 在运行中其值可变的量被称为变量 ...
- 第06组 Beta冲刺(1/4)
队名:福大帮 组长博客链接: https://www.cnblogs.com/mhq-mhq/p/11990568.html 作业博客 : https://edu.cnblogs.com/campus ...
- 运维笔记--Linux查找指定目录下某段时间的文件
查找指定目录下,60天之前的文件:find /mnt/xml_data -mtime +60 -name "*.xml" 找到并统计数量:find /mnt/xml_data -m ...
- win10磁盘分区
注意:未分配的磁盘是无法使用的. 安装系统的时候,我们可以先将系统装起来,再来对未分配的磁盘进行操作. 具体步骤:https://jingyan.baidu.com/article/fcb5aff76 ...
- SNP功能注释网站合集
这篇文章是对SNP位点功能注释在线网站的一个总结帖. 软件排名不分先后,优先顺序可以看推荐指数. 彩蛋在最后,请坚持看完 1.GWAS4D, 推荐指数:**** 网址:http://mulinlab. ...
- Postman:上传文件测试
1.设置好接口地址与提交方法 2.设置header Content-Type:multipart/form-data 3.设置body 选择为:form-data key要和服务器端保持一致 选择好自 ...
- 查找k8s版jenkins-slave官方镜像
官方镜像非常多,如果查找某个单词没有找到的话,可以换一个词查找,总之各种非常的多,带maven.djk.kubectl工具的镜像,都去试试吧, 从下面查找结果中可以看到,还有centos版的jenki ...
- js的style.width取不到元素的宽度值
使用jquery的.width()方法获取一个元素的当前宽度,不管元素是否设置了宽度,css样式时内联 外联或者是内嵌,都可以使用 这个方法获取元素的宽度. 使用原生js style.width .w ...