【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], ...
随机推荐
- C++ EH Exception(0xe06d7363)---捕获过程
书接上文<C++ EH Exception(0xe06d7363)----抛出过程>,下面我们讲下,VC++是如何catch到异常且处理的. 我们知道,在VC++里,C++异常实现的底层机 ...
- .NET体系结构
主要内容包括: C#与.NET的关系.公共语言运行库.中间语言.程序集..NET Framework类.名称空间.内层管理... C#与.NET的关系 C#是门高级编程语言,.NET(Framewor ...
- circus 架构
转自官方文档:https://circus.readthedocs.io/en/latest/design/architecture/ Overall architecture Circus is c ...
- Redis-5.0.5集群配置
版本:redis-5.0.5 参考:http://redis.io/topics/cluster-tutorial. 集群部署交互式命令行工具:https://github.com/eyjian/re ...
- 用户画像(User Profile)
什么是用户画像? 用户画像是根据某个具体的用户的人口学特征.网络浏览内容.网络社交活动和消费行为等信息而抽象出的一个标签化的用户模型.例如某用户的画像是:男,31岁,已婚,收入1万以上,爱美食,团购达 ...
- Time of Trial
Time of Trial(思维) 题目大意:一个人想逃,一个人要阻止那个人逃跑 ,阻止者念每一个符咒的时间为b[i],逃跑者念每一个符咒的时间为a[i],逃跑者如果念完了第i个符咒,阻止者还没有念完 ...
- 树莓派基于scratch2控制GPIO
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/raspberry_scratch2_gpio_control.m ...
- QML学习(一)——<简要概念知识点>
转载:https://www.cnblogs.com/dengyg0710/p/10644936.html 1.一个 QML 文档有且只有一个根元素. 2.QML 元素名后所有内容使用 {} 包围起来 ...
- Hadoop(三)—— YARN
YARN产生的背景 Hadoop相关概念 Hadoop 1.0 由HDFS.MapReduce组成. Hadoop 2.0 克服1.0中HDFS和MapReduce存在的各种问题而提出的. YARN是 ...
- Nginx配置简单负载均衡
前提:因为本次需要两个软件在本机不同端口提供http服务,于是我准备一个tomcat在8080端口,另一个nodejs程序在3000端口,前者自不用提,后者可以到https://www.cnblogs ...