leetcode--Two Sum II
来源:互联网 发布:淘宝衣服拍照价格 编辑:程序博客网 时间:2024/05/16 13:38
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
题意:给你一个升序排列的数组和一个target,在数组中找出两个和为target的整数。
返回这两个整数的index,并且要求index1小于index2
注意,index是基于1开始的,也就是说数组的第一个index是1,而不是0
分类:数组
解法1:这个题目比Two Sum http://blog.csdn.net/crazy__chen/article/details/45441671
还要简单,因为数组已经排好序了,用两个指针分别从头尾开始找就可以了。
如果头尾和大于target,说明尾太大,小于target说明头太小。
根据大小移动指针即可。
原文链接http://blog.csdn.net/crazy__chen/article/details/47338641
- [leetcode] Two Sum II
- LeetCode : Two Sum II
- leetcode [Two Sum II
- 【LeetCode】Two Sum II
- 【leetcode】Two Sum II
- LeetCode:Two Sum II
- leetcode--Two Sum II
- leetcode Two Sum II
- leetcode -- Two Sum II
- leetCode-Two Sum II
- 【Leetcode】【python】Two Sum, Two Sum II
- LeetCode-167. Two Sum II
- Leetcode #167 Two Sum II
- leetcode 167. Two Sum II
- [leetcode]167. Two Sum II
- leetcode 167. Two Sum II
- leetcode 167. Two Sum II
- LeetCode | 167. Two Sum II
- UI自动化测试框架对比(二)
- HTML学习-2
- Alfred for window添加workflow不起作用
- python创建文件和文件夹
- csdn博客利用github打造自己的专属域名
- leetcode--Two Sum II
- hdu2639 Rikka with Competition
- spring boot + fastjson
- LINK:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Java编写多人聊天室
- org.apache.log4j.helpers.Loader加载资源文件
- 中国剩余定理算法详解(余数互质和不互质)
- IntelliJ IDEA 14 注册码
- 包命名规范只Util,Tool,Service