您的位置:首页 > 编程语言 > Python开发

Python leetcode记录(1) -Two Sum

2017-08-23 18:43 525 查看
leetcode官网: 传送门

Two Sum

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:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].


我的答案: 基于python

class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
# 这里是我的答案
for i in range(len(nums)):

x = target - nums[i]
nums.remove(nums[i])
if x in nums:

return [i, i+nums.index(x)+1]


略坑,上面的答案是错的。

进行改进,但是感觉还是太复杂了一些。

import copy
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
n = copy.deepcopy(nums)
for i in range(len(nums)):

x = target - n[i]
nums.remove(n[i])
if x in nums:

return [i, i+nums.index(x)+1]


8月24号9:12再次改进,不使用deepcopy

class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(len(nums)):

x = target - nums[0]
nums.remove(nums[0])
if x in nums:
return [i, i + nums.index(x) + 1]


解题思路:

已知列表[1,3,5,7]和目标6,遍历列表的元素,先取第一个元素,6-1=5,判断5是否在列表中,如果在列表中,则返回第一个元素的位置和5的位置,如果不在列表中,把此元素从列表中移除,继续循环,直到返回为止。谨记题目说有且只有一个解决方案。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息