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

这道题描述的是:
 
给我们一个 非降序的数组 和 一个 目标值
要我们在 数组中 找到两个数的 索引位置,  这两个数满足 相加的值是目标值
 
并且 案例保证 一定有唯一的答案。不会出现多个和无解
要求 数组中每个元素只能使用一次
 
我的思路:
因为答案是唯一的,不会出现多个  也不会没有 所以省去很多步骤。
我从start(初始为0) 和 last(最后一个位置) 向中间找
如果 相加大于目标  last左移
如果 相加小于目标 start右移
 
相等的时候 返回 start 和 end 
 
我的python代码:
 class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
start, end = 0 , len(numbers) - 1
while True:
if numbers[start] + numbers[end] > target :
end -= 1
elif numbers[start] + numbers[end] < target :
start += 1
else :
break
return (start+1 , end+1 ) if __name__ == '__main__':
s = Solution()
res = s.twoSum([2,3,4],6)
print(res)
 
 
 

leetcode算法:Two Sum II - Input array is sorted的更多相关文章

  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 求两数相加等于一个数的位置 --------- java

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

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

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

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

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

  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. 进入TP-Link路由器之后利用快捷键F12查看星号路由密码的方法

    今天又破解了几个路由器,这两张图片是大多数路由器如TP-LINK路由器查看拨号圆点密码的方法.

  2. MapReduce浅析

    很早之前就用过Hadoop,但对MapReduce中的具体数据流向过程一直不甚明了,用Python Streamming的方式写了几个MapReduce,对这个过程有了一定的认识. 首先我们知道,Ma ...

  3. cookie 的增加,销毁,读取

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  4. CVPR2018: Unsupervised Cross-dataset Person Re-identification by Transfer Learning of Spatio-temporal Patterns

    论文可以在arxiv下载,老板一作,本人二作,也是我们实验室第一篇CCF A类论文,这个方法我们称为TFusion. 代码:https://github.com/ahangchen/TFusion 解 ...

  5. java web 项目中获取当前路径的几种方法

    1.jsp中取得路径:   以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.getRequestURI() 结果:/TEST/test.jsp (2)得到工程名:req ...

  6. 初探JodaTime

    在学习java之初时就使用过jdk自带的java.util.Calendar ,近期的项目中需要达到类似功能的时候使用了JodaTime. Joda-Time 令时间和日期值变得易于管理.操作和理解. ...

  7. select子句和三种子查询

    一.select子句 五种子句 Where.group by.having.order by.limit Where.group by.having.order by.limit运用的这个顺序不能变 ...

  8. oracle 分析函数中 keep关键字的使用

    语法 min | max(column1) keep (dense_rank first | last order by column2) over (partion by column3); 另外f ...

  9. Linux下的 >, >>, <, ps, |, grep, /dev/null

    1 要将命令行运行的结果保存到文件中,truncate模式下使用 >,append模式下使用 >> ls > ~/test.txt 2 要将文件中的内容作为标准输入,应使用 & ...

  10. React Native 轻松集成统计功能(iOS 篇)

    最近产品让我加上数据统计功能,刚好极光官方支持数据统计 支持了 React Native 版本 第一步 安装: 在你的项目路径下执行命令: npm install janalytics-react-n ...