python leetcode 1
开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目.
提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者.
1. Two Sum.
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
tmp = []
for i, j in enumerate(nums):
tmp.append((i, j)) for k, item in enumerate(tmp):
l, m = item
for o, p in tmp[k+1:]:
if m + p == target:
return sorted((l, o))
runtime beats: 21.30%
优化1
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i, j in enumerate(nums):
for k, l in enumerate(nums[i+1:]):
if j + l == target:
return (i, k + i + 1)
runtime beats: 21.97%
还是不行, 肯定还有别的办法.
2. Add Two Numbers
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
def gen(listnode):
yield listnode.val
while listnode.next:
listnode = listnode.next
yield listnode.val def izip_longest(gen1, gen2):
flag1, flag2 = True, True
try:
i = gen1.next()
except:
i = 0
flag1 = False try:
j = gen2.next()
except:
j = 0
flag2 = False
yield i, j while flag1 or flag2:
try:
i = gen1.next()
except:
i = 0
flag1 = False try:
j = gen2.next()
except:
j = 0
flag2 = False if flag1 or flag2:
yield i, j result = []
tmp = 0
for i, j in izip_longest(gen(l1), gen(l2)):
sum = i + j + tmp
if sum < 10:
tmp = 0
result.append(sum)
else:
tmp = 1
result.append(sum - 10)
if tmp:
result.append(tmp)
return result
runtime beats: 95.07%
用 python 刷题是有点不要脸了, 本题的关键是 itertools.izip_longest, 但是平台说不认识, 于是就自己攒了一个极端丑陋的版本. 有时间回头再优化.
3. Longest Substring Without Repeating Characters
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
if not s:
return 0 tmp = set(s)
length_tmp = len(tmp)
length = len(s) while length_tmp > 0:
for i in xrange(length - length_tmp + 1):
t = s[i:length_tmp + i]
if len(set(t)) < len(t):
continue if set(t) <= tmp:
return length_tmp
length_tmp -= 1
4. Median of Two Sorted Arrays
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
tmp = sorted(nums1 + nums2)
length = len(tmp)
half = length / 2.0
if int(round(half)) == int(half):
return (tmp[int(half) - 1] + tmp[int(half)]) / 2.0
else:
return tmp[int(half)]
...需要看看 sorted 的源码, 这样刷题有什么意思, 完全是在作弊.
5. Longest Palindromic Substring
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if not s:
return None def reverse1(string):
return string[::-1] def reverse2(string):
tmp = list(string)
tmp.reverse()
return "".join(tmp) length = len(s)
for i in xrange(length, 0, -1):
for j in xrange(length - i + 1):
tmp = s[j:i+j]
if tmp == reverse1(tmp):
return tmp
Time Limit Exceeded
python 判定回文很简单 string == string[::-1], 但是时间超时了, 暂时想不到更好的方法, 继续下一题.
6. ZigZag Conversion
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
tmp = []
if numRows == 1:
return s if len(s) <= numRows:
return s def attempt(gen):
tag = True
try:
rslt = gen.next()
except:
rslt = ""
tag = False
return tag, rslt def izip_longest(gen1, gen2):
gen1 = iter(gen1)
gen2 = iter(gen2) tag1, rslt1 = attempt(gen1)
tag2, rslt2 = attempt(gen2) yield rslt1, rslt2 while tag1 or tag2:
tag1, rslt1 = attempt(gen1)
tag2, rslt2 = attempt(gen2) if rslt1 or rslt2:
yield rslt1, rslt2 step = (numRows - 1) * 2
for i in xrange(numRows):
if(i == 0 or i == numRows - 1):
tmp.extend(s[i::step])
else:
j = (numRows - 1 - i) * 2
for _ in izip_longest(s[i::step], s[i+j::step]):
tmp.extend(_) return "".join(tmp)
7. Reverse Integer
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
# what's the hell?
if x > 2**31 - 1:
return 0 if x < -2**31:
return 0 if x >= 0:
tmp = list(str(x))
tmp.reverse()
rslt = int("".join(tmp))
return rslt if -2**31 <= rslt <= 2**31 -1 else 0
else:
tmp = list(str(0 - x))
tmp.reverse()
rslt = 0 - int("".join(tmp))
return rslt if -2**31 <= rslt <= 2**31 -1 else 0
8. String to Integer (atoi)
class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
def search(lst, s):
i = 0
for j in lst:
if j == s:
i += 1
return i def parse(s):
if not s:
return 0
if s == "+" or s == "-":
return 0
if search(s, "+") + search(s, "-") > 1:
return 0
else:
rslt = int(s)
if rslt > 2**31 -1:
return 2**31 -1
if rslt < -2**31:
return -2**31
return rslt str = str.strip()
if not str:
return 0 tmp = []
for i in str:
if i in ("+", "-") or i.isdigit():
tmp.append(i)
else:
break
string = "".join(tmp)
return parse(string)
9. Palindrome Number
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x < 0 or x > 2**31 - 1:
return False tmp = list(str(x))
if tmp == tmp[::-1]:
if int("".join(tmp[::-1])) > 2**31 -1 :
return False
else:
return True
else:
return False
12. Integer to Roman
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
def convert(value, ten, five, one):
rslt = []
if not value:
return ""
if value == 9:
rslt.append(one + ten)
elif value >= 5:
rslt.append(five + one * (value - 5))
elif value == 4:
rslt.append(one + five)
else:
rslt.append(one * value) return "".join(rslt) thousand = num // 1000
handred = (num - thousand * 1000) // 100
decade = (num - thousand * 1000 - handred * 100) // 10
unit = (num - thousand * 1000 - handred * 100 - decade * 10) result = []
if thousand:
result.append("M" * thousand) result.append(convert(handred, 'M', 'D', 'C'))
result.append(convert(decade, 'C', 'L', 'X'))
result.append(convert(unit, 'X', 'V', 'I')) return "".join(result)
13. Roman to Integer
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
result = 0
tmp = list(s) def get(lst, num):
try:
return lst[num]
except:
return None def calculate(tmp, i, j):
if j == "M":
return 1000 if j == "D":
return 500 if j == "L":
return 50 if j == "V":
return 5 if j == "C" and get(tmp, i+1) not in ("M", "D"):
return 100
elif j == "C" and get(tmp, i+1) in ("M", "D"):
return -100 if j == "X" and get(tmp, i+1) not in ("C", "L"):
return 10
elif j == "X" and get(tmp, i+1) in ("C", "L"):
return -10 if j == "I" and get(tmp, i+1) not in ("X", "V"):
return 1
elif j == "I" and get(tmp, i+1) in ("X", "V"):
return -1 for i, j in enumerate(tmp):
result += calculate(tmp, i, j) return result
14. Longest Common Prefix
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
""" # os.path.commonprefix if not strs:
return "" shortest = strs[0] for i in strs:
if len(shortest) > len(i):
shortest = i for i in xrange(len(shortest), 0, -1):
tag = True
tmp = shortest[:i]
for j in strs:
if j[:i] != tmp:
tag = False
if tag:
return tmp
return ""
15. 3Sum
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return [] length = len(nums) rslt = []
for i in xrange(0, length):
v_i = nums[i] for j in xrange(i+1, length):
v_j = nums[j] for k in xrange(j+1, length):
v_k = nums[k] if v_i + v_j + v_k == 0:
rslt.append(tuple(sorted([v_i, v_j, v_k]))) return list(set(rslt))
Time Limit Exceeded
16. 3Sum Closest
class Solution(object):
def threeSumClosest(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if not nums:
return [] length = len(nums) mapping = {}
rslt = set()
for i in xrange(0, length):
v_i = nums[i] for j in xrange(i+1, length):
v_j = nums[j] for k in xrange(j+1, length):
v_k = nums[k]
_abs = abs(v_i + v_j + v_k - target)
mapping[_abs] = v_i + v_j + v_k
rslt.add(_abs) return mapping[min(rslt)]
Time Limit Exceeded
17. Letter Combinations of a Phone Number
class Solution(object):
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
def multi(lst1, lst2):
if not lst1:
return lst2 rslt = []
for i in lst1:
for j in lst2:
rslt.append(i + j)
return rslt def worker(lst):
rslt = []
for i in lst:
rslt = multi(rslt, i)
return rslt mapping = {
2: "abc",
3: "def",
4: "ghi",
5: "jkl",
6: "mno",
7: "pqrs",
8: "tuv",
9: "wxyz"
} if not digits:
return [] if len(digits) == 1:
return list(mapping[int(digits)]) lst = [mapping[int(_)] for _ in digits if 1 < int(_) <= 9] return worker(lst)
18. 4Sum
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
if not nums:
return [] length = len(nums) rslt = []
for i in xrange(0, length):
v_i = nums[i] for j in xrange(i+1, length):
v_j = nums[j] for k in xrange(j+1, length):
v_k = nums[k] for l in xrange(k+1, length):
v_l = nums[l] if v_i + v_j + v_k + v_l == target:
rslt.append(tuple(sorted([v_i, v_j, v_k, v_l]))) return list(set(rslt))
Time Limit Exceeded
19. Remove Nth Node From End of List
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
def gen(node):
yield node.val
while node.next:
node = node.next
yield node.val lst = [_ for _ in gen(head)]
del lst[-n] return lst
20. Valid Parentheses
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
left = ["(", "{", "["]
right = [")", "}", "]"]
tmp = [] for i in s:
if i in left:
tmp.append(i)
if i in right:
index = right.index(i)
try:
j = tmp.pop()
except:
return False
else:
if j != left[index]:
return False if not tmp:
return True
else:
return False
21. Merge Two Sorted Lists
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1 and not l2:
return []
if not l1 and l2:
return l2
if l1 and not l2:
return l1 def gen(node):
yield node.val
while node.next:
node = node.next
yield node.val return sorted(list(gen(l1)) + list(gen(l2)))
感受到了一些题目的模式, 需要恶补一下数据结构和算法的基础知识和套路.
python leetcode 1的更多相关文章
- python leetcode 日记 --Contains Duplicate II --219
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- python LeetCode 两数相除
近一个月一直在写业务,空闲时间刷刷leetcode,刷题过程中遇到了一道比较有意思的题目,和大家分享. 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使 ...
- python leetcode 日记 --Contains Duplicate --217
题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...
- Python [Leetcode 345]Reverse Vowels of a String
题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
- Python LeetCode
Python不熟悉 不同的做法 404. Sum of Left Leaves 这是我的做法,AC. class Solution(object): res = 0 def recursive(sel ...
- python leetcode 颠倒二进制数
我的做法,,这个题在于必须补0 def reverseBits(n): num=32-len(bin(n)[2:]) m = bin(n)[2:][::-1] if num > 0: for i ...
- python(leetcode)-重复元素算法题
leetcode初级算法 问题描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 该问题表述非常简单 ...
- python(leetcode)-344反转字符串
编写一个函数,其作用是将输入的字符串反转过来.输入字符串以字符数组 char[] 的形式给出. 不要给另外的数组分配额外的空间,你必须原地修改输入数组.使用 O(1) 的额外空间解决这一问题. 你可以 ...
- python(leetcode)-48旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
随机推荐
- ORB-SLAM(六)回环检测
上一篇提到,无论在单目.双目还是RGBD中,追踪得到的位姿都是有误差的.随着路径的不断延伸,前面帧的误差会一直传递到后面去,导致最后一帧的位姿在世界坐标系里的误差有可能非常大.除了利用优化方法在局部和 ...
- java编码
/** * @Comments:default:ISO-8859-1 -> UTF-8 乱码则转码 * @param: str(乱码字符),coding(default:ISO- ...
- Django models .all .values .values_list 几种数据查询结果的对比
Django models .all .values .values_list 几种数据查询结果的对比
- Yii2开启enableprettyurl(url美化)无效
最终显示的url格式为:http://localhost/yii2/frontend/web/site/about 在/config/main.php中 'components'=>[] 中添加 ...
- Python检测服务端口存活状态并报警
最近发现公司的测试环境中有个Socket服务的端口总是莫名其妙Down掉,但是服务却正常运行着,看样子是僵死了... 虽然是测试环境,但是也不能这样放着不管,于是连夜写了一个简单的监控脚本.因为服务器 ...
- Java学习笔记15
do-while循环时while循环的变体语法如下:do{ // 循环体 语句(组);}while(循环继续条件); 如果循环中的语句至少需要执行一次,那么建议使用do-while循环. for循环 ...
- Pivot Table
1. Disable menu 'Disable show/hide Field list menu sht.PivotTables().EnableFieldList = False ''scrip ...
- 微信jsApI及微信分享对应在手机浏览器的调用总结。
摘录自别人的博客: 第一篇:微信内置浏览器的JsAPI(WeixinJSBridge续) 之前有写过几篇关于微信内置浏览器(WebView)中特有的Javascript API(Javascript ...
- PHP数组函数--array_filter
(PHP 4 >= 4.0.6, PHP 5, PHP 7) array_filter - 用回调函数过滤数组中的单元 (PHP 4 >= 4.0.6, PHP 5, PHP 7) arr ...
- lcok-free简易实现
lock-free是一种基于原子变量类来构建的非阻塞同步算法. 比较并交换(compare-and-swap) 我们经常会先检查某项东西,然后对其进行修改,如if(X...) {X=...}.这种行为 ...