LeetCode Two Sum II - Input array is sorted
原题链接在这里:https://leetcode.com/problems/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. 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.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
题解:
说指针从两头向中间递进. 类似Two Sum.
Time Complexity: O(n). Space: O(1).
AC Java:
public class Solution {
public int[] twoSum(int[] numbers, int target) {
int [] res = new int[]{-1,-1};
if(numbers == null || numbers.length < 2){
return res;
}
int l = 0;
int r = numbers.length-1;
while(l<r){
if(numbers[l] + numbers[r] < target){
l++;
}else if(numbers[l] + numbers[r] > target){
r--;
}else{
res[0] = l+1;
res[1] = r+1;
break;
}
}
return res;
}
}
LeetCode Two Sum II - Input array is sorted的更多相关文章
- [LeetCode] 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 <面试常考题>
题目描述 //二分查找的变形 用头尾两个指针进行 面试考察题 class Solution { public: vector<int> twoSum(vector<int> ...
- 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 ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 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], ...
- 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 ...
随机推荐
- A hard puzzle
A hard puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 向 ViewPager 中添加 包含 ListView 的 Fragment
对与fragment就不说什么了,直接看API手册吧,亲. 向 ViewPager 中添加 包含 ListView 的 Fragment 的过程比较麻烦.他所表现的效果就是新闻客户端的滑动翻页效果. ...
- Nginx设置Js、Css等静态文件的缓存过期时间
location ~.*\.(js|css|html|png|jpg)$ { expires 3d; } expires 3d; //表示缓存3天 expires 3h; //表示缓存3小 ...
- Leetcode Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- SVN版本控制工具使用学习
SVN版本控制工具使用学习 Subversion是优秀的版本控制工具. 1.下载和搭建SVN服务器 http://subversion.apache.org/packages.html 类型有5种,推 ...
- HTML5 Geolocation 构建基于地理位置的 Web 应用
HTML5 中的新功能 HTML5 是最新一代的 HTML 规范,是 W3C 与 WHATWG 合作的结果,目前仍外于开发中.自从上一代 HTML4,Web 世界已经发生了巨大的变化,HTML5 的到 ...
- NOI模拟赛Day2
深深的感受到了自己的水 ---------------------------------------------------------------------------------------- ...
- Windows环境下的NodeJS+NPM+Bower安装配置步骤
Windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到首页的“INSTALL”按钮 ...
- Node.js 手册查询-1-核心模块方法
Node.js 学习手册 标签(空格分隔): node.js 模块 核心模块 核心模块是被编译成二进制代码,引用的时候只需require表示符即可 os 系统基本信息 os模块可提供操作系统的一些基本 ...
- kinderEditor + Struts2整合
环境: kinderEditor4.1.5 Struts2.3.5 Spring3.0.5 Hibernate3.6 代码: FileManageAction package com.hcsoft.p ...