摘要: 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+ 阅读(181) 评论(0) 推荐(0) 编辑
摘要: 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+ 阅读(198) 评论(0) 推荐(0) 编辑
摘要: 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+ 阅读(217) 评论(0) 推荐(0) 编辑
摘要: 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+ 阅读(155) 评论(0) 推荐(0) 编辑
摘要: 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+ 阅读(226) 评论(0) 推荐(0) 编辑
摘要: 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+ 阅读(132) 评论(0) 推荐(0) 编辑
摘要: 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+ 阅读(128) 评论(0) 推荐(0) 编辑
摘要: 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+ 阅读(99) 评论(0) 推荐(0) 编辑
摘要: 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+ 阅读(164) 评论(0) 推荐(0) 编辑
摘要: 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+ 阅读(133) 评论(0) 推荐(0) 编辑
摘要: 方法一:深度优先搜索 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+ 阅读(165) 评论(0) 推荐(0) 编辑
摘要: 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+ 阅读(226) 评论(0) 推荐(0) 编辑