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.
 class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
my_dict = {}
for i, num in enumerate(numbers):
remain = target - num
if remain in my_dict:
index = my_dict[remain]
return [index, i + 1]
my_dict[num] = i + 1
return [-1, -1]

最新文章

  1. android基础(六)android的消息处理机制
  2. CPlus的简单线程的制作
  3. Linux第八次学习笔记
  4. 【IHttpHandler】使用IHttpHandler防盗链
  5. 最新 Arduino 驱动 12接口/户外 LED显示屏/LED点阵屏/LED单元板
  6. Java学习小结(1)-数组的创建与传参
  7. JavaScript基础-面向对象编程<1>
  8. WebLech是一个功能强大的Web站点下载与镜像工具
  9. CentOS7 已安装telnet维修
  10. openstack私有云布署实践【12.2 网络Neutron-controller节点配置(办公网环境)】
  11. 老李分享:Python开发性能测试脚本
  12. stick footer布局
  13. spring mvc报错,数据库查询无限死循环
  14. JVM-垃圾收集的过程
  15. TCP的三次握手过程与四次挥手
  16. 【MM系列】SAP的库存管理
  17. maven中jar下载失败
  18. npm 发布包(publish)
  19. numpy 数据处理
  20. artTemplate 原生 js 模板语法版

热门文章

  1. Ubuntu Hadoop使用过程中的一些技巧1
  2. rabbitmq设置队列消息存活时间
  3. Python 安装gevent,在导入gevent之后就报错了
  4. Odd sum (对本菜鸡来说是个极坑题)
  5. gitlab命令详解
  6. 5. react 基础 - 组件拆分 和 组件传值
  7. 基于JSP+Servlet新闻发布系统 源码
  8. JNI的第2种写法:本地方法注册
  9. 一文彻底搞懂Cookie、Session、Token到底是什么
  10. 吴裕雄--天生自然Linux操作系统:Linux 文件基本属性