博客
关于我
两数之和
阅读量:408 次
发布时间:2019-03-05

本文共 481 字,大约阅读时间需要 1 分钟。

‘’’

两数之和
给定一个已按照升序排列的有序数组,找到两个数使得它们相加之和等于你目标数

函数应该返回这两个下标值 index1和index2 其中index1必须小于index2    说明        返回的下标值(index1和index2)不是从零开始的        你可以假设每个输入只对应唯一的答案 而且你不可以重复使用相同的元素

实例

输入 numbers = [2,7,11,15]
target = 9
输出:
[1,2]
‘’’

双指针数

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]

target = 17
index1 = 0
index2 = len(nums)-1

while index1 < index2:

if nums[index1]+nums[index2] == target:
print(index1 + 1, index2 + 1)
break
elif nums[index1]+nums[index2] > target:
index2 -= 1
else:
index1 += 1

转载地址:http://gxwzz.baihongyu.com/

你可能感兴趣的文章
netstat kill
查看>>
netstat命令用法详解
查看>>
Netstat端口占用情况
查看>>
Netty 4的内存管理:sun.misc.Unsafe
查看>>
Netty channelRegistered\ChannelActive---源码分析
查看>>
Netty NIO transport && OIO transport
查看>>
netty php,netty
查看>>
Netty WebSocket客户端
查看>>
netty 主要组件+黏包半包+rpc框架+源码透析
查看>>
Vue过渡 & 动画---vue工作笔记0014
查看>>
Netty 异步任务调度与异步线程池
查看>>
Netty 的 Handler 链调用机制
查看>>
Netty 编解码器和 Handler 调用机制
查看>>
Netty 编解码器详解
查看>>
Netty 解决TCP粘包/半包使用
查看>>
Netty 调用,效率这么低还用啥?
查看>>
Netty 高性能架构设计
查看>>
Netty+Protostuff实现单机压测秒级接收35万个对象实践经验分享
查看>>
Netty+SpringBoot+FastDFS+Html5实现聊天App详解(一)
查看>>
netty--helloword程序
查看>>