【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], ...
随机推荐
- 【DataStage】使用Sequence Job报错:CopyOfseq_ld..JobControl (fatal error from @Coordinator): Sequence job (restartable) will abort due to previous unrecoverable errors
错误描述: 在使用Sequence Job加载作业的时候,报了个错,详细错误内容如下: 出现这个错误的原因是由于以下配置问题,Excution action的状态为Run造成. 解决方案: 将Excu ...
- [Beta]第七次 Scrum Meeting
[Beta]第七次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/17 22:00 10min 大运村公寓6F寝室 附Github仓库:WEDO 例会照片 工作情况 ...
- 第06组 Beta冲刺(3/4)
队名:福大帮 组长博客链接: 作业博客 : https://edu.cnblogs.com/campus/fzu/SoftwareEngineeringClassAofFuzhouUniversity ...
- Python 实现毫秒级淘宝、京东、天猫等秒杀抢购脚本
本篇文章主要介绍了Python 通过selenium实现毫秒级自动抢购的示例代码,通过扫码登录即可自动完成一系列操作,抢购时间精确至毫秒,可抢加购物车等待时间结算的,也可以抢聚划算的商品. 该思路可运 ...
- python 图片格式转换png转jpg,如何利用python给图片添加半透明水印
from PIL import Imageim = Image.open(r'd:\test2.png')r, g, b, a = im.split()im = Image.merge("R ...
- 错误: -source 1.6 中不支持 diamond 运算符
问题 错误: -source 1.6 中不支持 diamond 运算符 解决步骤 1.检查ide的默认编译环境 ,快捷键ctrl + alt +s 找Java Compiler ,发现设置是 Targ ...
- 使用PLSQL导入excel数据至oracle数据库
https://blog.csdn.net/qq_42909551/article/details/82108754 https://jingyan.baidu.com/album/14bd256e2 ...
- c#怎么解决System.UnauthorizedAccessException异常
https://blog.csdn.net/qq_38061677/article/details/81157116 代码: using System;namespace Project2048{ c ...
- linux shell中如何批量添加一行内容到某些文件的末尾?
答:先使用find找出要指定的某些文件,然后使用xargs和sed工具将内容插入到这些文件的末尾 find . -name 'filename*' | xargs sed -i '$a\added-c ...
- git 比较实用的命令
git 删除已经add 过的文件 使用 git rm 命令即可,有两种选择, 使用 git rm 命令即可,有两种选择, 一种是 git rm --cached "文件路径",不删 ...