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. .NET Core入门程序及命令行练习

    用命令行一步一步新建项目.添加Package.Restore.Build.Run 执行的实现方式,更让容易让我们了解.NET Core的运行机制. 准备工作 安装.NET Core 运行环境,下载地址 ...

  2. springcloud - bus

    在重新设置了后的bootstrap.yml和application.yml后,可以看到bus-refresh的端点请求了.在之前bootstrap也可以设定哪个端点是可见,哪个未见. 如: #actu ...

  3. rollup node.js 打包工具

    最近在做一个提供给浏览器和node同时使用的js的url模板工具类,在用什么打包工具上纠结了一段时间,正好有一天在知乎上看到了关于rollup的介绍,在自己试了试之后,就决定用rollup.js来打包 ...

  4. shell脚本编程之变量的小用法

    变量赋值 ${parameter:-word}:如果parameter为空或未定义,则变量展开为"word":否则,展开为parameter的值: ${parameter:+wor ...

  5. ssh修改默认远程端口

    ---------------------centos6-----------------1.查看系统版本cat /etc/redhot-releose 2.编辑sshd配置,修改默认的端口vim / ...

  6. GIT 安装和升级

    git 安装: http://git-scm.com/download/mac git 升级: $ git clone git://git.kernel.org/pub/scm/git/git.git

  7. Compute API 关键概念 详解

    Compute API 是 RESTful HTTP 服务,提供管理虚机的能力. 虚机可能有不同的内存大小,CPU数量,硬盘大小,能够在几分钟之内创建出来.和虚机的交互,可以通过Compute API ...

  8. 原生js实现浏览器全屏和退出全屏

    全屏模式 //W3C if (docElm.requestFullscreen) { docElm.requestFullscreen(); } //FireFox else if (docElm.m ...

  9. 隐藏一个button的方法(2种) 写出一个button的按钮(2种)

    display:none;visibility:hidden: <input type = button><button>这是一个按钮 </button>

  10. mysql增删改查sql语句

    未经允许,禁止转载!!!未经允许,禁止转载!!! 创建表   create table 表名删除表    drop table 表名修改表名   rename table 旧表名 to 新表名字创建数 ...