LeetCode : No1 Two Sum Python
来源:互联网 发布:js修改html属性值 编辑:程序博客网 时间:2024/05/17 20:35
题目链接:
https://leetcode.com/problems/two-sum/
考虑到效率问题,通过两层循环的方式,时间复杂度为O(n^2),时间溢出。
解法1:
利用python 自带的sorted排序,然后根据两数之和为target逐步从数组的两头向中间寻找,直到两数之后为target,再反定位数字在原数组的位置。
耗时:63ms
class Solution: # @return a tuple, (index1, index2) def twoSum(self, num, target): sortedNum = sorted(num) start = 0 end = len(sortedNum)-1 Sum = sortedNum[start]+sortedNum[end] while Sum != target: if Sum > target: end = end -1 else: start = start +1 Sum = sortedNum[start]+sortedNum[end] Min = num.index(sortedNum[start]) num[Min] = -float("inf") Max = num.index(sortedNum[end]) if Min < Max: return (Min+1,Max+1) else: return (Max+1,Min+1)
0 0
- LeetCode : No1 Two Sum Python
- leetcode No1. Two Sum
- LeetCode Problem No1 Two Sum
- No1.Two Sum
- 【LeetCode从零单排(Java)】No1.Two Sum
- 【Leetcode】【python】Two Sum, Two Sum II
- leetcode two sum python 实现
- 【LeetCode with Python】 Two Sum
- 1.Two Sum Leetcode Python
- [leetcode]-[Two Sum]-python实现
- [Leetcode.python] 001. Two Sum
- [Leetcode] 1.Two Sum @python
- leetcode-001-two sum-python
- Two Sum Leetcode Python Java
- LeetCode: 1. Two Sum python
- [LeetCode] Two Sum Python 题解
- Leetcode: 1. Two Sum [python]
- Python leetcode #1 Two Sum
- LeetCode 165. Compare Version Numbers
- 奇怪的FileNotFoundException
- win7下搭建ftp服务器
- nginx之gzip压缩
- Python学习(1)——Python文档简介
- LeetCode : No1 Two Sum Python
- google focebook twiwer
- bootstrap自带分页器的构造函数
- Linux 命令tr
- 差不vc呢你地方的多少
- AC3/EAC3格式总结
- XCode6.1 无法连接SVN https
- HDU1022 Train Problem I
- Matlab自己实现Sobel边缘提取和ROA边缘提取