随笔分类 - leetcode
leetcode刷题记录
摘要:简单题 class Solution: def strStr(self, haystack: str, needle: str) -> int: if needle=='': return 0 if len(needle)>len(haystack): return -1 if len(needle
阅读全文
摘要:class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: s='' if strs==[] : return '' if '' in strs: return '' for i in range(len(strs[0
阅读全文
摘要:简单题 class Solution: def addBinary(self, a: str, b: str) -> str: s=0 a=list(a) a=a[::-1] for i in range(1,len(a)): s+=int(a[i])*2**i s+=int(a[0]) t=0 b
阅读全文
摘要:真是不容易。。。 class Solution: def myPow(self, x: float, n: int) -> float: if x==float(1): return 1 if x float(1) : if n%2==1:return -1 else:return 1 if n>=
阅读全文
摘要:还需优化。。。 class Solution: def intToRoman(self, num: int) -> str: memo={'I':1,'IV':4,'V':5,'IX':9,'X':10,'XL':40,'L':50,'XC':90,'C':100,'CD':400,'D':500,
阅读全文
摘要:简单题: 用栈实现: class Solution: def isValid(self, s: str) -> bool: if s=='': return True if len(s)==1: return False stack=[] i=0 while i<len(s): if s[i] in
阅读全文
摘要:这道题我还是没思路,看了别人的做法觉得豁然开朗简单明了!!!! 栈: class Solution: def longestValidParentheses(self, s: str) -> int: if not s: return 0 res=[] stack=[] for i in range
阅读全文
摘要:class Solution(object): def isMatch(self, s, p): """ :type s: str :type p: str :rtype: bool """ #'.' 匹配任意单个字符 #'*' 匹配零个或多个前面的那一个元素 memo=dict()#创建空字典 d
阅读全文
摘要:class Solution: def lengthOfLastWord(self, s: str) -> int: if s==' '*len(s): return 0 if ' ' not in s: return len(s) a=s.split(' ') i=-1 while a[i]=='
阅读全文
摘要:class Solution: def numDecodings(self, s: str) -> int: a={} if int(s[0])==0: return 0 else: a[0]=1 if len(s)==1: return a[0] if int(s[0])>2 and int(s[
阅读全文
摘要:class Solution: def minPathSum(self, grid) -> int: m=len(grid) n=len(grid[0]) if m==0 or n==0: return 0 if m==1 and n==1: return grid[0][0] for i in r
阅读全文
摘要:class Solution: def uniquePathsWithObstacles(self, obstacleGrid) -> int: if len(obstacleGrid)<1: return 0 m=len(obstacleGrid) n=len(obstacleGrid[0]) i
阅读全文
摘要:class Solution: def uniquePaths(self, m: int, n: int) -> int: memo=[[0 for i in range(n)] for i in range(m)]#定义空二维数组 for i in range(n): memo[0][i]=1 f
阅读全文
摘要:可能是找到了对的学习方法,更有感觉,就清晰地做出来就觉得很开心。 class Solution(object): def rotate(self, matrix): """ :type matrix: List[List[int]] :rtype: None Do not return anythi
阅读全文
摘要:我真的是超开心了,又做对了!!!!!!而且没走啥弯路!!!!!!! class Solution(object): def jump(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums)<2: return 0 pac
阅读全文
摘要:class Solution(object): def firstMissingPositive(self, nums): """ :type nums: List[int] :rtype: int """ if nums==[]: return 1 for i in nums: if i<=0:
阅读全文
摘要:好开心!!!我做到了!!!! 第一次独立完成回溯+剪枝。 class Solution(object): def combinationSum2(self, candidates, target): """ :type candidates: List[int] :type target: int
阅读全文
摘要:给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的数字可以无限制重复被选取。 说明: 所有数字(包括 target)都是正整数。解集不能包含重复的组合。 示例 1: 输入:
阅读全文
摘要:用时29分09秒: 滑动窗口虽然不懂具体是什么概念,但是用到的应该是这个思路: public int lengthOfLongestSubstring(String s) { int n = s.length(); if(n<2) return n; int i =0,j=1,max_len=1;
阅读全文
摘要:我太难了,俩小时。。。。。。 class Solution(object): def isNumber(self, s): """ :type s: str :rtype: bool """ s=s.strip() for i in range(len(s)): if s[i] not in 'e0
阅读全文

浙公网安备 33010602011771号