leetcode 167 two sum II
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 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.
Note:
- 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.
Example:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2. 双指针问题,由于是排过序的列表,可以左指针指向起始,右指针指向末尾。两个指针根据条件收缩。最终找到符合的两个值。
如果numbers[left] + numbers[right] 大于target,则右指针左移,使和减小。
如果numbers[left] + numbers[right] 小于target,则左指针右移,使和增大。
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
left = 0
right = len(numbers) - 1
while left < right:
add = numbers[left] + numbers[right]
if add == target:
return [left + 1, right + 1]
elif add < target:
left = left + 1
else:
right = right - 1
leetcode 官网有效率更高的代码,感兴趣的可以搜下。
leetcode 167 two sum II的更多相关文章
- 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 : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [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 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 ...
随机推荐
- python学习笔记_week18
note 1.JS 正则 test - 判断字符串是否符合规定的正则 rep = /\d+/; rep.test("asdfoiklfasdf89asdfasdf") # true ...
- PHP 服务 php-fpm 的一些常见配置
< 操作系统 Centos7,PHP版本7.2.7 > 已下所有配置涉及到时间单位均使用 => 秒(s) 分 (m) 时 (h) 天(d) [ 以下为全局配置 ] 01,关于,进程文 ...
- react-native-vector-icons 图标库使用
安装链接 yarn add react-native-vector-icons react-native link react-native-vector-icons 在项目工程中打开 .xcodep ...
- 电脑组装DIY
技嘉主板:B150M-D3H 网卡驱动: CPU: 风扇: 机箱: 显示器:
- HTML 圆心节点
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vmware 不使用物理内存运行缓慢的处理方法
VMware虚拟机直接使用物理内存的方法 1:打开虚拟机操作系统文件夹,找到.vmx后缀的文件 2:以记事本方式打开文件,在最后一行添加mainMem.useNamedFile=FALSE. 3:保存 ...
- ElasticSearch match, match_phrase, term区别
1.term结构化字段查询,匹配一个值,且输入的值不会被分词器分词. 比如查询条件是: { "query":{ "term":{ "foo" ...
- 5.Python文件操作之增删改查
需求一:取文件的前几行: f = open("yesterday","r",encoding="utf-8") for i in range ...
- Linux性能测试分析命令_sar
sar主要用于收集并统计系统资源的信息,包括CPU.IO.内存.网卡流量等. sar语法 用法:sar [ 选项 ] [ <时间间隔> [ <次数> ] ] 常用选项说明: - ...
- vue项目遇到的坑
一.启动项目问题 1. 如何从git上拉下项目:点我 2. 启动项目失败: 点我 and 点我 二.搭建项目问题 1. 先改分辨率,否则可能影响布局 以我的项目为例,分辨率修改位置如下: 2. .v ...