LeetCode & Q167-Two Sum II - Input array is sorted-Easy
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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- (双指针 二分) 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 ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 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 ...
- ✡ 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- RedHat 7.3 更新yum源
title: RedHat 7.3 更新yum源 time: 2018.3.15 查看所有yum包 [root@bogon ~]# rpm -qa | grep yum yum-rhn-plugin- ...
- AJAX跨域问题解决思路
ajax跨域问题的解决思路主要分为3种: 1.浏览器限制解决思路:不让浏览器做出限制解决方法:通过指定参数,让浏览器不做跨域校验评价:价值不大,需要每个人都做改动,而且改动是客户端的改动 2.XHR请 ...
- 去除IE10自带的清除按钮
最近在工作中碰到了一个问题,原本在IE8,IE9下正常的input表单,在IE10下会出现清除按钮,即表单右侧会出现一个可以清除该表单内容的小叉.由于之前一直没有兼容过IE10,所以我专门搜了下原因. ...
- xampp打开显示缺少运行库的解决方法
如图:,安装好xampp的时候直接打开会弹出上面那个错误,显示的原因是因为缺少运行库,点击"确定"之后会弹出一个网页,上面有解决方法:http://www.phpstudy.net ...
- nginx里的sticky的作用
1.Sticky工作原理 : Sticky是nginx的一个模块,它是基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上,默认标识 ...
- http状态码是什么,有什么用,在哪里查看,分别代表什么意思?
写在前面: 当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头用以响应浏览器的请求.本文主要是:关于ht ...
- python安装第三方库
在编写爬虫程序时发现unsolved import 一时不解,以为是ide出问题了,其实是没有安装第三方库导致的. 于是到https://pypi.python.org/pypi/requests/去 ...
- WinSock 异步I/O模型
如果你想在Windows平台上构建服务器应用,那么I/O模型是你必须考虑的. Windows操作系统提供了五种I/O模型,分别是选择(select)模型,异步选择(WSAAsyncSelect)模型, ...
- webpack学习
// 一个常见的`webpack`配置文件 const webpack = require('webpack'); const HtmlWebpackPlugin = require('html-we ...
- (转)Android 仿订单出票效果 (附DEMO)
之前我下载了BaseAnimation 开源库(BaseAnimation是基于开源的APP,致力于收集各种动画效果) BaseAnimation 转载的链接:http://blog.csdn.net ...