leetcode1408
摘要:1 class Solution: 2 def stringMatching(self, words: 'List[str]') -> 'List[str]': 3 n = len(words) 4 words = sorted(words,key=lambda x:len(x)) 5 res =
阅读全文
posted @
2020-04-12 12:41
Sempron2800+
阅读(185)
推荐(0)
leetcode396
摘要:C++的实现: 1 class Solution { 2 public: 3 int maxRotateFunction(vector<int>& A) { 4 long N = A.size(); 5 long S = 0; 6 long t = 0; 7 for (int i = 0; i <
阅读全文
posted @
2020-04-09 10:43
Sempron2800+
阅读(193)
推荐(0)
leetcode393
摘要:1 class Solution: 2 def validUtf8(self, data): 3 # 标记这个字节是某个编码的第几个字节 4 n_bytes = 0 5 6 # 遍历数组 7 for num in data: 8 9 # 获取二进制编码,保留最低8位 10 bin_rep = for
阅读全文
posted @
2020-04-09 10:42
Sempron2800+
阅读(208)
推荐(0)
leetcode390
摘要:1 class Solution { 2 public: 3 int lastRemaining(int n) { 4 if (n == 1) return 1; 5 return 2 * (n / 2 + 1 - lastRemaining(n / 2)); 6 7 // vector<int>
阅读全文
posted @
2020-04-09 10:40
Sempron2800+
阅读(223)
推荐(0)
leetcode388
摘要:1 class Solution(object): 2 def lengthLongestPath(self, input): 3 """ 4 :type input: str 5 :rtype: int 6 """ 7 input = input.split('\n') 8 res = 0 9 s
阅读全文
posted @
2020-04-09 10:34
Sempron2800+
阅读(166)
推荐(0)
leetcode373
摘要:1 class Solution: 2 def kSmallestPairs(self, nums1, nums2, k): 3 queue = [] 4 def push(i, j): 5 if i < len(nums1) and j < len(nums2): 6 heapq.heappush
阅读全文
posted @
2020-04-09 10:30
Sempron2800+
阅读(237)
推荐(0)
leetcode368
摘要:1 class Solution(object): 2 def largestDivisibleSubset(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: List[int] 6 """ 7 # The container that hol
阅读全文
posted @
2020-04-09 10:27
Sempron2800+
阅读(139)
推荐(0)
leetcode372
摘要:1 class Solution: 2 def superPow(self, a: int, b: List[int]) -> int: 3 return pow(a,int(''.join(map(str,b))),1337) 算法思路:直接调用python的pow函数。 学有余力的同学,可以尝试
阅读全文
posted @
2020-04-09 10:18
Sempron2800+
阅读(135)
推荐(0)
leetcode386
摘要:1 class Solution: 2 def lexicalOrder(self, n: int) -> List[int]: 3 lis = [str(i) for i in range(1,n+1)] 4 lis.sort() 5 return lis 算法思路:按字符串顺序排序。 当然有能力
阅读全文
posted @
2020-04-09 10:12
Sempron2800+
阅读(106)
推荐(0)
leetcode385
摘要:1 class Solution: 2 def deserialize(self, s: str) -> NestedInteger: 3 4 if s[0] != '[': 5 return NestedInteger(int(s)) 6 7 stack = [] 8 # num为数字,sign为
阅读全文
posted @
2020-04-09 09:44
Sempron2800+
阅读(174)
推荐(0)
leetcode397
摘要:1 class Solution: 2 def integerReplacement(self, n: int) -> int: 3 count = 0 4 while n != 1: 5 if (n & 1) == 0: # 偶数直接右移 6 n >>= 1 7 else: 8 n += -1 i
阅读全文
posted @
2020-04-09 09:41
Sempron2800+
阅读(135)
推荐(0)
leetcode365
摘要:方法一:深度优先搜索 1 class Solution: 2 def canMeasureWater(self, x: int, y: int, z: int) -> bool: 3 stack = [(0, 0)] 4 self.seen = set() 5 while stack: 6 rema
阅读全文
posted @
2020-04-09 09:30
Sempron2800+
阅读(169)
推荐(0)
leetcode355
摘要:1 import heapq 2 import collections 3 class Twitter: 4 def __init__(self): 5 self.followers = collections.defaultdict(set)#key是被关注者,value是关注这个用户的人的集合
阅读全文
posted @
2020-04-09 08:54
Sempron2800+
阅读(232)
推荐(0)
leetcode332
摘要:没能做出来,参考别人的: 1 from heapq import heapify, heappush, heappop 2 3 class Solution: 4 def findItinerary(self, tickets: List[List[str]]) -> List[str]: 5 gr
阅读全文
posted @
2020-04-08 20:18
Sempron2800+
阅读(193)
推荐(0)
leetcode331
摘要:1 class Solution: 2 def isValidSerialization(self, preorder: str) -> bool: 3 if preorder == '#':#空树是合法的 4 return True 5 nodes = preorder.split(',') 6
阅读全文
posted @
2020-04-08 17:54
Sempron2800+
阅读(184)
推荐(0)
leetcode313
摘要:1 import sys 2 class Solution: 3 def nthSuperUglyNumber(self, n: int, primes: 'List[int]') -> int: 4 dp = [1]*n 5 m = len(primes) 6 plist = [0] * m 7
阅读全文
posted @
2020-04-08 16:15
Sempron2800+
阅读(161)
推荐(0)
leetcode310
摘要:尝试使用bfs解决,但是TLE。给出代码如下: 1 import sys 2 class Solution: 3 def __init__(self): 4 self.depth = [] 5 self.mindepth = sys.maxsize 6 7 def bfs(self,n,visite
阅读全文
posted @
2020-04-08 11:53
Sempron2800+
阅读(160)
推荐(0)
leetcode306
摘要:1 class Solution: 2 def backTrack(self,num,l,idx,n): 3 if idx >= n: 4 if len(l) >= 3: 5 return True 6 else: 7 return False 8 for i in range(idx,n): 9
阅读全文
posted @
2020-04-07 21:49
Sempron2800+
阅读(181)
推荐(0)
leetcode307
摘要:1 class segTree: 2 def __init__(self,nums): 3 n = len(nums) 4 for i in range(1,31): 5 low,high = 2 ** (i-1),2 ** i 6 if n > low and n < high: 7 fillze
阅读全文
posted @
2020-04-07 13:40
Sempron2800+
阅读(160)
推荐(0)
leetcode304
摘要:1 class NumMatrix: 2 def __init__(self, matrix: 'List[List[int]]'): 3 self.matrix = matrix 4 m,n = 0,0 5 m = len(matrix) 6 if m > 0: 7 n = len(matrix[
阅读全文
posted @
2020-04-07 09:41
Sempron2800+
阅读(158)
推荐(0)
leetcode45
摘要:1 class Solution(object): 2 def jump(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 ln = len(nums) 8 curr = 0 9 last = 0 10 step = 0
阅读全文
posted @
2020-04-06 10:10
Sempron2800+
阅读(166)
推荐(0)
leetcode1405
摘要:第一种方案,使用堆: 1 from heapq import heappush, heappop 2 class Solution: 3 def longestDiverseString(self, a: int, b: int, c: int) -> str: 4 max_heap = [] 5
阅读全文
posted @
2020-04-06 08:50
Sempron2800+
阅读(257)
推荐(0)
leecode220
摘要:1 class Solution: 2 def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool: 3 if t == 0 and len(set(nums)) == len(nums): 4 r
阅读全文
posted @
2020-04-06 08:48
Sempron2800+
阅读(119)
推荐(0)
leetcode275
摘要:1 class Solution: 2 def hIndex(self, citations): 3 citations_len = len(citations) 4 if citations_len<=0: 5 return 0 6 low = 0 7 high = citations_len-1
阅读全文
posted @
2020-04-06 08:44
Sempron2800+
阅读(168)
推荐(0)
leetcode274
摘要:1 class Solution: 2 def hIndex(self, citations: 'List[int]') -> int: 3 n = len(citations) 4 if n == 0: 5 return 0 6 citations = sorted(citations,rever
阅读全文
posted @
2020-04-06 08:41
Sempron2800+
阅读(107)
推荐(0)
leetcode284
摘要:1 class PeekingIterator: 2 def __init__(self, iterator): 3 self.iterator = iterator 4 self.head = iterator.next() 5 6 def peek(self): 7 return self.he
阅读全文
posted @
2020-04-06 08:16
Sempron2800+
阅读(144)
推荐(0)
leetcode1404
摘要:1 class Solution: 2 def convertInt(self,s): 3 n = len(s) 4 basenum = 0 5 p = 0 6 for i in range(n-1,-1,-1): 7 basenum += int(s[i]) * (2 ** p) 8 p += 1
阅读全文
posted @
2020-04-05 12:57
Sempron2800+
阅读(179)
推荐(0)
leetcode1403
摘要:1 class Solution: 2 def minSubsequence(self, nums: 'List[int]') -> 'List[int]': 3 nums = sorted(nums,reverse=True) 4 n = len(nums) 5 if n == 1: 6 retu
阅读全文
posted @
2020-04-05 12:55
Sempron2800+
阅读(170)
推荐(0)
leetcode264
摘要:先给出一个我自己写的方案,TLE 1 class Solution: 2 def __init__(self): 3 self.memo = dict() 4 self.memo[1] = True 5 6 def isUgly(self,num): 7 if num <= 0: 8 return
阅读全文
posted @
2020-04-05 09:53
Sempron2800+
阅读(220)
推荐(0)
leetcode229
摘要:1 class Solution: 2 def majorityElement(self, nums: List[int]) -> List[int]: 3 dic = dict() 4 n = len(nums) 5 for i in range(n): 6 if nums[i] not in d
阅读全文
posted @
2020-04-05 09:25
Sempron2800+
阅读(136)
推荐(0)
leetcode1401
摘要:1 class Solution: 2 def checkOverlap(self, radius: int, x_center: int, y_center: int, x1: int, y1: int, x2: int, y2: int) -> bool: 3 4 # Getting the c
阅读全文
posted @
2020-04-05 05:05
Sempron2800+
阅读(154)
推荐(0)
leetcode1400
摘要:1 class Solution: 2 def canConstruct(self, s: str, k: int) -> bool: 3 #奇数个字符出现的数量 <= k 4 #所有的字符的类别数量 >= k 5 dic = dict() 6 n = len(s) 7 for i in range
阅读全文
posted @
2020-04-05 05:00
Sempron2800+
阅读(142)
推荐(0)
leetcode1399
摘要:1 class Solution: 2 def calSum(self,x): 3 s = str(x) 4 sums = 0 5 for j in range(len(s)): 6 sums += int(s[j]) 7 return sums 8 9 def countLargestGroup(
阅读全文
posted @
2020-04-05 04:55
Sempron2800+
阅读(172)
推荐(0)
leetcode228
摘要:1 class Solution: 2 def summaryRanges(self, nums: List[int]) -> List[str]: 3 n = len(nums) 4 if n == 0: 5 return [] 6 pre = nums[0] 7 res = [[nums[0],
阅读全文
posted @
2020-04-04 20:34
Sempron2800+
阅读(147)
推荐(0)
leetcode223
摘要:1 class Solution: 2 def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int: 3 x = 0 4 y = 0 5 6 if (A <= E): 7 i
阅读全文
posted @
2020-04-04 10:14
Sempron2800+
阅读(143)
推荐(0)
leetcode222
摘要:1 class Solution: 2 def __init__(self): 3 self.count = 0 4 5 def preOrder(self,node): 6 if node != None: 7 self.count += 1 8 self.preOrder(node.left)
阅读全文
posted @
2020-04-04 09:45
Sempron2800+
阅读(150)
推荐(0)
leetcode211
摘要:1 class TrieNode: 2 def __init__(self): 3 self.words = 0 4 self.edges = [None] * 26 5 6 7 class WordDictionary: 8 def __init__(self): 9 self.root = Tr
阅读全文
posted @
2020-04-04 08:48
Sempron2800+
阅读(156)
推荐(0)
leetcode209
摘要:1 class Solution { 2 public int minSubArrayLen(int s, int[] nums) { 3 int idx1=0,idx2=0; 4 int currSum=0; 5 int ret=Integer.MAX_VALUE; 6 while(idx2<nu
阅读全文
posted @
2020-04-03 09:31
Sempron2800+
阅读(185)
推荐(0)