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的更多相关文章

  1. LeetCode算法题-Two Sum II - Input array is sorted

    这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...

  2. 【Leetcode 167】Two Sum II - Input array is sorted

    问题描述:给出一个升序排列好的整数数组,找出2个数,它们的和等于目标数.返回这两个数的下标(从1开始),其中第1个下标比第2个下标小. Input: numbers={2, 7, 11, 15}, t ...

  3. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  4. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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], ...

随机推荐

  1. hibernate的持久化类、主键生成策略

    一.hibernate的持久化类 1.什么是持久化类: 持久化:将数据存储到关系型数据库. 持久化类:与数据库中的数据表建立了某种关系的java类.(持久化类=javabean+映射配置文件) 2.持 ...

  2. Docker 安装ubuntu服务器

    ### 1. 安装ubuntu ```docker pull ubuntudocker run -it -d --name ubuntu_test -p 2222:22 ubuntu ``` ### ...

  3. 48、Spark SQL之与Spark Core整合之每日top3热点搜索词统计案例实战

    一.概述 1.需求分析 数据格式: 日期 用户 搜索词 城市 平台 版本 需求: 1.筛选出符合查询条件(城市.平台.版本)的数据 2.统计出每天搜索uv排名前3的搜索词 3.按照每天的top3搜索词 ...

  4. 通过Zabbix监控Tomcat单机多实例

    前面已经介绍过Tomcat单机多实例部署,接下来就在他的基础上进行下一步操作:Tomcat多实例监控! Tomcat多实例监控过程和之前的redis多实例原理一样,分为以下4步: 1.获取多实例 2. ...

  5. Java使用Jsoup之爬取博客数据应用实例

    导入Maven依赖 <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --> <dependency> <g ...

  6. layui如何自定义弹出层关闭事件

    在某些业务场景下,我们需要自定义弹出层关闭事件,代码示例如下: layui.use('layer', function () { var layer = layui.layer; layer.open ...

  7. ubuntu之路——day14 只用python的numpy在底层实现多层神经网络

    首先感谢这位博主整理的Andrew Ng的deeplearning.ai的相关作业:https://blog.csdn.net/u013733326/article/details/79827273 ...

  8. 团队作业-Beta版本演示

    组长博客链接 https://www.cnblogs.com/cmlei/p/12063671.html 本组成员 031702431 陈明磊 组长 031702227 林镕炜 031702413 韩 ...

  9. 正则表达式在线分析 regex online analyzer

    https://regexper.com/#%2F%5B0-9%5D%5Cs%5B0-9%5D%2F https://regexper.com/ http://regexone.com/lesson/ ...

  10. Java_jdbc 基础笔记之十一数据库连接 (通用的查询方法)

    鉴于之前的查询方法,在这里我们可以写一个通用的方法 /** * 鉴于 student.和customer查询的方法有好多相同之处,在此可以写一个通用的方法 */ public <T> T ...