Array Two Pointers Binary Search

Description:

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. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution and you may not use the same element twice.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

其实我是只会最暴力的直接穷举法的,参考了July大神的文章,搞懂了这题tag里的Two Pointers到底是什么意思。在这道题里,指的就是用两个指针在首尾分别往中间扫描,通过条件来判定哪一边的指针需要移动。

my Solution:

public class Solution {
public int[] twoSum(int[] numbers, int target) {
int n = numbers.length;
int[] output = new int[2];
int i = 0, j = n-1;
while (i < j) {
if (numbers[i] + numbers[j] > target) {
j--;
} else if (numbers[i] + numbers[j] < target) {
i++;
} else {
output[0] = ++i;
output[1] = ++j;
break;
}
}
return output;
}
}

对于这道经典题,July提出了好几种解法,分别为:

  • 二分,时间复杂度$O(NlogN)$ 空间复杂度$O(1)$
  • 扫描一遍X-numbers[i]映射到一个hash表,Discuss里有人用这种方法,其实是二叉树查找的思想,不过时间复杂度虽然变为$O(N)$,但由于要创建hash表,空间复杂度为$O(N)$
  • 两指针从两端向中间扫描,就是Solution的解法

LeetCode & Q167-Two Sum II - Input array is sorted-Easy的更多相关文章

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

  2. [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  3. LeetCode 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 the ...

  4. (双指针 二分) leetcode 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 the ...

  5. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

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

    Problem: Given an array of integers that is already sorted in ascending order, find two numbers such ...

  7. ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  8. Java [Leetcode 167]Two Sum II - Input array is sorted

    题目描述: Given an array of integers that is already sorted in ascending order, find two numbers such th ...

  9. LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

    1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...

  10. LeetCode 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 the ...

随机推荐

  1. 【Chrome控制台】获取元素上绑定的事件信息以及监控事件

    需求场景 在前端开发中,偶尔需要验证下某个元素上到底绑定了哪些事件,以及监控某个元素上的事件触发情况. 解决方案 普通操作 之前面对这种情况,一般采取的措施就是在各个事件里写console.info, ...

  2. AWS 认证攻略(SA)

    很高兴经过一个多月的努力顺利pass了自己的SA认证,同事说证都是虚的,不过考个证也算是对自己实力的认可吧,博主第一次写博文,先简单的写一些认证的攻略吧 1.博主11月正式入职云服务提供商,领导要求每 ...

  3. TC命令流量控制测试(针对具体IP和具体进程)

    TC命令流量控制测试 这里测试系统为Linux操作系统,通过简单的TC命令来实现对带宽的控制. 1对具体IP地址的流量控制 这里采用iperf来进行带宽的测试,首先在服务器和客户端都安装上iperf软 ...

  4. python中Django 使用方法简述

    Django是由Python写成的免费而且开源的Web应用框架--一堆零件的组成,可以帮助我们轻松的开发网站.这些零件都包括常用的:登录(注册,登入,登出),网站后台管理,表单,文件上传等.可以帮助我 ...

  5. 内嵌tomcat启动速度慢

    项目上最近要把内置的jetty换成tomcat, 来更好的支持servlet 3.0 本来以为换个容器, 几十行代码就好了. 实际上换了tomcat后, 一开始启动tomcat, 非常的慢. jett ...

  6. Global.asax 中校验Session

    Application 相关的 Application_Init:在每一个HttpApplication实例初始化的时候执行. Application_Disposed:在每一个HttpApplica ...

  7. JS报表打印分页CSS

    在调用window.print()时,可以实现打印效果,但内容太多时要进行分页打印. 在样式中有规定几个打印的样式 page-break-before和page-break-after CSS属性并不 ...

  8. 快递查询 C#

    //电商ID private string EBusinessID = "1257164"; //电商加密私钥,快递鸟提供,注意保管,不要泄漏 private string App ...

  9. loadrunner 录制中文出现乱码的解决办法

  10. centos7的服务管理

    1,启动服务(每条都可以)systemctl start httpdsystemctl start httpd.serviceservice httpd start 2,停止服务systemctl s ...