1. package y2019.Algorithm.array;
  2.  
  3. /**
  4. * @ProjectName: cutter-point
  5. * @Package: y2019.Algorithm.array
  6. * @ClassName: TwoSum2
  7. * @Author: xiaof
  8. * @Description: 167. Two Sum II - Input array is sorted
  9. * 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.
  10. * 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.
  11. *
  12. * Input: numbers = [2,7,11,15], target = 9
  13. * Output: [1,2]
  14. * Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
  15. *
  16. * @Date: 2019/7/2 10:22
  17. * @Version: 1.0
  18. */
  19. public class TwoSum2 {
  20.  
  21. public int[] solution(int[] numbers, int target) {
  22. //求两数之和就是对应的target,并且反馈对应的下标
  23. int index1 = 0, index2 = 0;
  24. //这里其实也是遍历,但是已知target,那么我们每次只要获取到第一个数据的下表,然后对后面的数据进行二分查找判断是否有对应的数据就可以了
  25. for(int i = 0; i < numbers.length; ++i) {
  26. index1 = i;
  27. int num1 = numbers[i];
  28. //二分查找目标
  29. int targetNum = target - num1;
  30. int start = i + 1, end = numbers.length - 1;
  31. while(start < end) {
  32. int mid = start + ((end - start) >>> 1);
  33. if(numbers[mid] == targetNum) {
  34. index2 = mid;
  35. break;
  36. } else if (numbers[mid] > targetNum) {
  37. //如果目标位置比目标数据大,说明要找的数据在前面
  38. end = mid;
  39. } else {
  40. //如果比目标数据小,说明再后面,然后我们mid的位置已经做了比较,所以后移一位
  41. //因为mid = start + (end - start) >>> 1; 可能正好等于start
  42. start = mid + 1;
  43. }
  44. }
  45.  
  46. if(start == end) {
  47. //如果首尾相等,那么手动判断一下
  48. if(targetNum == numbers[start]) {
  49. index2 = start;
  50. }
  51. }
  52.  
  53. if(index2 != 0) {
  54. break;
  55. }
  56. }
  57.  
  58. int result[] = new int[2];
  59. result[0] = index1 + 1;
  60. result[1] = index2 + 1;
  61. return result;
  62. }
  63.  
  64. //网上大牛最快,也是占内存最小的算法,有点类似快排的思想
  65. public int[] twoSum(int[] numbers, int target) {
  66. int []res = new int [2];
  67. int index1 = 0;
  68. int index2 = numbers.length - 1;
  69. while (index2 > index1){
  70. if (numbers[index1] + numbers[index2] > target){
  71. index2 --;
  72. }
  73. else if(numbers[index1] + numbers[index2] < target){
  74. index1 ++;
  75. }else{
  76. res[0] = index1+1;
  77. res[1] = index2+1;
  78. break;
  79. }
  80. }
  81. return res;
  82. }
  83.  
  84. public static void main(String args[]) {
  85.  
  86. int pres[] = {5,25,75};
  87. int target = 100;
  88. System.out.println(new TwoSum2().solution(pres, target));
  89.  
  90. }
  91.  
  92. }

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

  2. [Beta]第七次 Scrum Meeting

    [Beta]第七次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/17 22:00 10min 大运村公寓6F寝室 附Github仓库:WEDO 例会照片 工作情况 ...

  3. 第06组 Beta冲刺(3/4)

    队名:福大帮 组长博客链接: 作业博客 : https://edu.cnblogs.com/campus/fzu/SoftwareEngineeringClassAofFuzhouUniversity ...

  4. Python 实现毫秒级淘宝、京东、天猫等秒杀抢购脚本

    本篇文章主要介绍了Python 通过selenium实现毫秒级自动抢购的示例代码,通过扫码登录即可自动完成一系列操作,抢购时间精确至毫秒,可抢加购物车等待时间结算的,也可以抢聚划算的商品. 该思路可运 ...

  5. python 图片格式转换png转jpg,如何利用python给图片添加半透明水印

    from PIL import Imageim = Image.open(r'd:\test2.png')r, g, b, a = im.split()im = Image.merge("R ...

  6. 错误: -source 1.6 中不支持 diamond 运算符

    问题 错误: -source 1.6 中不支持 diamond 运算符 解决步骤 1.检查ide的默认编译环境 ,快捷键ctrl + alt +s 找Java Compiler ,发现设置是 Targ ...

  7. 使用PLSQL导入excel数据至oracle数据库

    https://blog.csdn.net/qq_42909551/article/details/82108754 https://jingyan.baidu.com/album/14bd256e2 ...

  8. c#怎么解决System.UnauthorizedAccessException异常

    https://blog.csdn.net/qq_38061677/article/details/81157116 代码: using System;namespace Project2048{ c ...

  9. linux shell中如何批量添加一行内容到某些文件的末尾?

    答:先使用find找出要指定的某些文件,然后使用xargs和sed工具将内容插入到这些文件的末尾 find . -name 'filename*' | xargs sed -i '$a\added-c ...

  10. git 比较实用的命令

    git 删除已经add 过的文件 使用 git rm 命令即可,有两种选择, 使用 git rm 命令即可,有两种选择, 一种是 git rm --cached "文件路径",不删 ...