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. 从 HelloWorld 看 Java 字节码文件结构

    很多时候,我们都是从代码层面去学习如何编程,却很少去看看一个个 Java 代码背后到底是什么.今天就让我们从一个最简单的 Hello World 开始看一看 Java 的类文件结构. 在开始之前,我们 ...

  2. ES2015 类中的静态方法

    在ES2015中,终于不用用函数原型来实现类系统,可以直接使用关键字class,下面是对class的静态属性的研究: 举例:一个Node类,每一个Node类之间都可以建立从属关系,每一个Node实例下 ...

  3. 用python给html里的css及js文件链接自动添加版本号

    传统的给文件链接添加版本号的方法是使用gulp-rev,这里提出的解决方案是使用python来替代gulp-rev. import os import re import uuid import sy ...

  4. 一年java工作经验的面试题总结(持续更新中)

    本人是17年6月份毕业的,3月份出来实习,算起来也是工作一年了吧,金三银四,博主也考虑换一份工作,于是最近面试了几家,总结一下面试中的问题,大家一起交流学习. 第一次面试  ①说下java类的加载 ② ...

  5. Solidity constant view pure关键字的区别与联系

    在Solidity中constant.view.pure三个函数修饰词的作用是告诉编译器,函数不改变/不读取状态变量,这样函数执行就可以不消耗gas了(是完全不消耗!),因为不需要矿工来验证.所以用好 ...

  6. WordPress文章页添加展开/收缩功能

    很多时候我们在WordPress上发布一些文章的时候里面都包含了很多的代码,我一般又不喜欢把代码压缩起来而喜欢让代码格式化显示,但是格式化显示通常会让文章内容看起来很多,不便于访问者浏览,所以今天就介 ...

  7. python web开发-flask中日志的使用

    Flask使用日志记录的方式: 初始化flask应用实例 在flask中使用logger,需要初始化一个flask的应用 app = Flask(__name__) 2. 调用logger 直接调用l ...

  8. AngularJS应用,常用数组知识点

    AngularJS 1:ng-click,ng-model,ng-bind,ng-class,ng-hide,ng-app 2:placeholder, 3:{}中加入代码“:true|false”, ...

  9. 一、Android四大框架之ContentProvider的学习与运用,实现SQLite的增删改查。

    本文系原创博客,文中不妥烦请指出,如需转载摘要请注明出处! ContentProvider的学习与运用 Alpha Dog 2016-04-13  10:27:06 首先,项目的地址:https:// ...

  10. docker进阶-利用dcoker Swarm搭建简单集群

    什么是Swarm   在介绍Swarm之前我们要说一下什么Docker三剑客? Docker-Machine:负责在多种平台上快速安装 Docker 环境. Docker-Compose:Docker ...