首页 > 编程语言 > 详细

leetcode--1:(python)Two Sum

时间:2019-07-11 18:55:00      阅读:98      评论:0      收藏:0      [点我收藏+]

2019.5.25:  #1

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:技术分享图片

 

我的解法:

技术分享图片

  这种思维上比较直观,就是把索引号全做一个配对,按照配对逐个试。但是python是分离式链表,对于这种一直查表的操作十分缓慢。。。何况每次尝试都要查两次表

 

厉害的解法:利用字典,把元素作为key,该次尝试的索引号作为value,每次的差值都和key做比较相同就return不同就加入字典等待以后能匹配,每次只查一次表而且不会重复工作。

技术分享图片

 

leetcode--1:(python)Two Sum

原文:https://www.cnblogs.com/marvintang1001/p/11171889.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!