算法刷题
笔记
qq_42749341
这个作者很懒,什么都没留下…
展开
-
算法刷题12、剑指offer03:数组中重复的数字
class Solution: def findRepeatNumber(self, nums: [int]) -> int: i = 0 while i < len(nums): if nums[i] == i: i += 1 continue if nums[nums[i]] == nums[i]: return nums[i] .原创 2020-08-03 14:05:31 · 91 阅读 · 1 评论 -
算法刷题12、第11题盛最多谁的容器
class Solution: def maxArea(self, height: List[int]) -> int: i, j, res = 0, len(height) - 1, 0 while i < j: if height[i] < height[j]: res = max(res, height[i] * (j - i)) i += 1 ..原创 2020-07-27 11:09:00 · 124 阅读 · 0 评论 -
算法刷题11、第10题正则表达式匹配
class Solution(object): @lru_cache def isMatch(self, text, pattern): if not pattern: return not text first_match = bool(text) and pattern[0] in {text[0], '.'} # 匹配 * if len(pattern) >= 2 and patte...原创 2020-07-27 11:02:26 · 81 阅读 · 0 评论 -
算法刷题10、第8题 字符串转换整数
class Solution: def myAtoi(self, str: str) -> int: import re matches = re.match('[ ]*([+-]?\d+)', str) # 最重要的就是这一句了吧,正则重在搞定匹配的pattern if matches: res = int(matches.group(1)) if res > (MAX := 2 **..原创 2020-07-27 10:42:50 · 79 阅读 · 0 评论 -
算法刷题9、第392题 判断子序列
class Solution: def isSubsequence(self, s: str, t: str) -> bool: return True if not s else self.isSubsequence(s[1:], t[t.index(s[0])+1:]) if s[0] in t else False原创 2020-07-27 10:36:20 · 85 阅读 · 0 评论 -
算法刷题8、 第6题 Z 字形变换
class Solution: def convert(self, s: str, numRows: int) -> str: if numRows==1: return s hashrow=["" for _ in range(numRows)] k=0 flag=1 for c in s: if k==0: flag=1 .原创 2020-07-25 21:32:22 · 104 阅读 · 0 评论 -
算法刷题7、第5题 最长回文子串
class Solution: def expand(self, s, left, right): while left >= 0 and right < len(s) and s[left] == s[right]: left -= 1 right += 1 return (right - left - 2) // 2 def longestPalindrome(self, s: str) -&.原创 2020-07-25 21:29:50 · 123 阅读 · 0 评论 -
算法刷题6、第4题 寻找两个正序数组的中位数
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: if len(nums1) > len(nums2): return self.findMedianSortedArrays(nums2, nums1) infinty = 2**40 m, n = len(nums1), l.原创 2020-07-25 21:24:02 · 118 阅读 · 0 评论 -
算法刷题5、第13题 罗马数字转整数
代码: class Solution: def romanToInt(self, s: str) -> int: a = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} ans=0 for i in range(len(s)): if i<len(s)-1 and a[s[i]]<a[s[i+1]]: an..原创 2020-07-24 20:45:14 · 154 阅读 · 1 评论 -
算法刷题4 、第 7题 整数反转
class Solution: def reverse(self, x: int) -> int: if -2**31 <=x<= 2**31-1: a=str(x).replace('-','') a=int(a[::-1]) if a < 2**31: if x >= 0: return a .原创 2020-07-16 15:49:37 · 89 阅读 · 0 评论 -
算法刷题 3、第3题 无重复字符的最长子串
题目: class Solution: def lengthOfLongestSubstring(self, s): if s: resultList=[] length=[] item=0 i=0 while i<len(s): if s[i] in resultList: resul原创 2020-07-07 14:01:07 · 114 阅读 · 0 评论 -
算法刷题 2、第2题 两数相加
这是我写的,比较啰嗦 class Solution: def addTwoNumbers(self, l1, l2): p1=l1 p2=l2 jin=0 cur=ListNode(0) p=cur while p1 or p2: if jin==1: cur.val+=1 else: cur.原创 2020-07-07 08:23:36 · 228 阅读 · 0 评论 -
算法刷题 1、第1题 两数之和
方法1 两遍循环 def twoSum(self, nums, target): n=len(nums) for i in range(n): for j in range(n): if nums[i]+nums[j]==target: return [i,j] 时间:5896ms 空间:14.6MB 方法2 把第二次循环变为判断target-nums[i]是否存在 def twoS.原创 2020-07-02 10:35:52 · 171 阅读 · 0 评论