LeetCode 1.Two Sum (Python)
来源:互联网 发布:淘宝链接转成淘口令 编辑:程序博客网 时间:2024/06/06 17:26
题目描述:
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].
AC代码Python版:
class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] 更优秀的解 O(n) tmp_num = {} for i in range(len(nums)): if target - nums[i] in tmp_num: # here do not need to deal with the condition i = target-i return (tmp_num[target-nums[i]], i) else: tmp_num[nums[i]] = i return (-1, -1) """ result = [] isA = True for i in range(len(nums)): for j in range(i + 1, len(nums)): if nums[i] + nums[j] == target: result.append(i) result.append(j) isA = False break if isA is False: break return result
阅读全文
0 0
- LEETCODE 1.Two Sum (python实现)
- Leetcode题解(Python): 1.Two Sum
- LeetCode 1.Two Sum (Python)
- 【leetcode】1. Two Sum(Python & C++)
- 1.Two Sum Leetcode Python
- [Leetcode] 1.Two Sum @python
- LeetCode: 1. Two Sum python
- Leetcode: 1. Two Sum [python]
- [leetcode:python]1.Two Sum
- leetcode 1. Two Sum python
- 【Leetcode】【python】Two Sum, Two Sum II
- LeetCode:1. Two Sum解法(C,C++,Jave,Python)
- 【Leetcode】【Python】【C++】1. Two Sum实现
- LeetCode 1. Two Sum Python Solution
- 【LeetCode 1】Two Sum(Python)
- 1. Two Sum(leetcode)
- leetcode two sum python 实现
- 【LeetCode with Python】 Two Sum
- Elasticsearch学习:分片的理解
- python 无法加载downsample模型问题
- Android使用反射机制设置ListView的默认焦点
- Jquery简单地实现ajax
- 服务端与客户端交互(三)
- LeetCode 1.Two Sum (Python)
- 关于centos6.5安装kong网关的步骤
- O2O与B2B、B2C、C2C之间的区别
- Vue.js学习笔记:属性绑定 v-bind
- IOS
- C/C++引用外部函数的两种方式
- 特殊密码锁,openjudge全局题号8496,已AC
- zTree应用实例详讲(2)
- nyoj 104 最大和(子矩阵最大和)(枚举)