- 博客(34)
- 收藏
- 关注
原创 Leetcode 977. Squares of a Sorted Array (C++)
题目解法:class Solution {public: vector<int> sortedSquares(vector<int>& A) { // get the dividing position of the negative and positive elements int n = size(A); int div_pos=0; for (int i=0;i<n;i++){
2020-10-29 13:52:33 108
原创 Leetcode 44. Wildcard Matching & Regular Expression Matching (python)
Leetcode 44. Wildcard Matching解法:动态规划这道题目在于理解代码中的step2和step4的第一部分关于step 4的第一步份,实际上可以用0,1背包跟完全背包之前的关系来理解class Solution {public: bool isMatch(string s, string p) { int sl = s.length(); int pl = p.length(); // step1:
2020-10-29 13:48:23 132
原创 Leetcode 1562. Find Latest Group of Size M (python)
Leetcode 1562. Find Latest Group of Size M题目解法1:TLE解法2:题目解法1:TLE自己开始想出来的非常复杂的解法,还是TLE了,主要T在从list里面删除元素class Solution: def findLatestStep(self, arr: List[int], m: int) -> int: ans = -1 pos2len = {} len2pos = collections.d
2020-10-28 12:37:24 225
原创 Leetcode 1561. Maximum Number of Coins You Can Get
Leetcode 1561. Maximum Number of Coins You Can Get题目解法1:heap解法2:sort题目解法1:heap这道题的规律是,每次取剩下堆中的最大的两个值和最小的值组成triplet就可以,剩下的只是实现问题。当时自己做的时候是用一个最大堆和一个最小堆实现的,其实这样比较慢class Solution: def maxCoins(self, piles: List[int]) -> int: min_heap = pile
2020-10-28 12:24:10 166
原创 Leetcode 1560. Most Visited Sector in a Circular Track (python)
Leetcode 1560. Most Visited Sector in a Circular Track题目解法:brutal force解法2:题目解法:brutal force没有仔细思考规律就是写的这么丑陋class Solution: def mostVisited(self, n: int, rounds: List[int]) -> List[int]: d = collections.defaultdict(int) for i in
2020-10-28 09:53:13 239
原创 Leetcode 1630. Arithmetic Subarrays (python)
Leetcode 1630. Arithmetic Subarrays题目解法1:sort解法2:利用set题目解法1:sortclass Solution: def checkArithmeticSubarrays(self, nums: List[int], l: List[int], r: List[int]) -> List[bool]: def check(array): array.sort() dis = a
2020-10-27 09:59:54 145
原创 Leetcode 1631 Path With Minimum Effort (python)
Leetcode Path With Minimum Effort题目解析follow up题目解析这个题目再Leetcode 的problems里面还没有,只出现在Weekly contest 212的第3题一开始觉得这个题目是个DP,觉得两遍dp可以做,实际上思路错了,两个方向的dp是不行的。基本上看到四个方向可走的题目就是用最短路径问题来做,最短路径这边也提供两种做法,一种是Bellman ford,另一种是Dijkstra,关于两种做法的详细解释看这题 Leetcode The Ma
2020-10-27 09:41:43 988
原创 Leetcode 1568. Minimum Number of Days to Disconnect Island (python)
Leetcode 1568. Minimum Number of Days to Disconnect Island题目解析:题目解析:这道题目的关键就在于,想到只有可能是0,1,2三种答案,想到了这个题目也就非常简单了class Solution: def minDays(self, grid: List[List[int]]) -> int: # check if there is more than one disconnected islands in teh
2020-10-27 07:44:44 177
原创 Leetcode 1567. Maximum Length of Subarray With Positive Product (python)
Leetcode 1567. Maximum Length of Subarray With Positive Product题目:解法1:解法2:two pass题目:解法1:这种判断正负的题目往往都是通过正负得负,负负得正这样的方式来进行交换得到的,之前也有过类似的题目:比如Leetcode 152 Maximum Product Subarray这题具体的解析如下:The main idea here is to keep a count of running positive array
2020-10-25 06:12:33 481
原创 Leetcode 1578. Minimum Deletion Cost to Avoid Repeating Letters (python)
Leetcode 1578. Minimum Deletion Cost to Avoid Repeating Letters题目解法1:one pass解法题目解法1:略复杂版本的解法,一开始在contest的时候写的版本class Solution: def minCost(self, s: str, cost: List[int]) -> int: prefix = [0]*(len(cost)+1) tmp = 0 for i,
2020-10-23 04:37:35 465
原创 Leetcode 1615. Maximal Network Rank (python)
Leetcode 1615. Maximal Network Rank题目暴力解法:邻接矩阵预存边题目暴力解法:class Solution: def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int: graph = collections.defaultdict(int) for road in roads: graph[road[
2020-10-21 13:54:44 482
原创 Leetcode 1616. Split Two Strings to Make Palindrome (python+cpp)
Leetcode 1616. Split Two Strings to Make Palindrome题目:解法1:暴力,TLE解法2:O(n)题目:解法1:暴力,TLEclass Solution: def checkPalindromeFormation(self, a: str, b: str) -> bool: if len(a) == 1 or len(b) == 1: return True n = len(a)
2020-10-21 13:45:58 180
原创 Leetcode 1626. Best Team With No Conflicts (python)
Leetcode 1626. Best Team With No Conflicts题目解析:题目解析:对于这种由多个维度的题目,应该基本都是先对一个维度进行一些处理,比如排序,然后再看一个维度固定的情况下,另一个维度上看问题简化成了什么。或者说这个问题是由什么问题包装而来的这道题目将age进行排序之后,会发现问题简化成了求max sum of non-decreasing sub-sequence。与Leetcode 300. Longest Increasing Subsequence几乎是一
2020-10-19 14:00:03 373
原创 Leetcode 1584. Min Cost to Connect All Points (python)
Leetcode 1584. Min Cost to Connect All Points题目错误解法:解法:Minimum spanning tree题目错误解法:最开始以为通过判断加入的边的数量和被访问过的点的数量可以判断是不是符合条件了,其实这样是不成立的,可以举出很多反例class Solution(object): def minCostConnectPoints(self, points): """ :type points: List[Lis
2020-10-18 08:09:50 858
原创 Leetcode 863. All Nodes Distance K in Binary Tree (python)
Leetcode 863. All Nodes Distance K in Binary Tree题目解析:题目解析:这道题看到固定距离寻找这种限制条件,第一个想到的就应该是BFS。所以这道题目变成了两个部分:将树转化为图,每个节点指向parent节点和左右子节点从target节点出发,BFS寻找特定距离的点值得注意的是,这边给的target是节点,所以不需要额外去寻找target节点这边将树转化为图有两种做法,一种是利用树的性质,将parent直接作为树节点的属性,代码如下:cla
2020-10-15 10:27:49 246
原创 Leetcode 98. Validate Binary Search Tree (python)
Leetcode 98. Validate Binary Search Tree题目解法:题目解法:首先二叉搜索树的定义熟悉一下。对于一个节点,他可能出现这样的情况:是父节点的左节点,又是祖父节点的右子树中的节点 (意味着这个节点要比父节点小,但是要比祖父节点大)是父节点的右节点,又是祖父节点左子树中的节点 (意味着这个节点要比父节点大,但是要比祖父节点小)从上面的情况分析可以看出,对于一个节点,有一个上边界和下边界,而且这个边界会根据节点的深度变化不断更新。这就是解法的思路class
2020-10-15 09:59:45 219
原创 Leetcode 347. Top K Frequent Elements & 692. Top K Frequent Words (python)
题目解法1:排序 (NlogN)class Solution(object): def topKFrequent(self, nums, k): """ :type nums: List[int] :type k: int :rtype: List[int] """ count = collections.Counter(nums) candidates = [(-v,key) fo
2020-10-14 00:52:44 153
原创 Leetcode 17. Letter Combinations of a Phone Number(python+cpp)
Leetcode 17. Letter Combinations of a Phone Number题目解析题目解析这题实际上是一个无重复combination的模板题,只需要对1做一下特殊处理即可关于combination和permutation的总结,看这里:代码:class Solution: def letterCombinations(self, digits: str) -> List[str]: def backtracking(comb,level
2020-10-13 06:37:16 297
原创 Leetcode 1593. Split a String Into the Max Number of Unique Substrings(python)
Leetcode 1593. Split a String Into the Max Number of Unique Substrings题目解法:暴力backtracking题目解法:暴力backtracking因为要unique,所以dp不太做得了class Solution: def maxUniqueSplit(self, s: str) -> int: def backtracking(ind): nonlocal ans
2020-10-12 07:50:58 215
原创 Leetcode 152&1594 maximum product (python)
Leetcode 152. Maximum Product Subarray解法:同时储存最大的正值和最小的负值,下一个最大值一定会在跟这两个的乘积中出现class Solution(object): def maxProduct(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return 0
2020-10-12 07:38:49 105
原创 Leetcode 22. Generate Parentheses (python)
Leetcode 22. Generate Parentheses题目解法:backtrackingfollow up题目解法:backtrackingclass Solution: def generateParenthesis(self, n: int) -> List[str]: def backtracking(comb,left,right): # if left==right, means one valid combination
2020-10-09 13:50:19 355
原创 Leetcode 32. Longest Valid Parentheses (python+cpp)
Leetcode 32. Longest Valid Parentheses题目解法1:stack解法2:两次扫描题目解法1:stack解释参见commentclass Solution: def longestValidParentheses(self, s: str) -> int: # add -1 as the starting of a valid sequence stack = [-1] max_length = 0
2020-10-09 13:47:34 131
原创 Leetcode 1599. Maximum Profit of Operating a Centennial Wheel (python)
Leetcode 1599. Maximum Profit of Operating a Centennial Wheel题目解法:题目题目太长,直接放链接吧 https://leetcode.com/problems/maximum-profit-of-operating-a-centennial-wheel/解法:仔细地读懂题目不难做,就是greedy的思想一开始写了一个非常丑的版本:class Solution: def minOperationsMaxProfit(self, c
2020-10-08 12:56:28 138
原创 Leetcode Binary Tree Preorder, inorder, postorder Traversal (python)
Leetcode Binary Tree Preorder, inorder, postorder Traversal题目解析:题目https://leetcode.com/problems/binary-tree-preorder-traversal/https://leetcode.com/problems/binary-tree-inorder-traversal/submissions/https://leetcode.com/problems/binary-tree-postorder-t
2020-10-08 12:44:30 98
原创 Leetcode 1600. Throne Inheritance (python)
Leetcode 1600. Throne Inheritance题目解法:follow up题目题目太长了,直接放链接吧 https://leetcode.com/problems/throne-inheritance/解法:这道题目实际上是个树的preorder visit,然后储存的时候用字典的方式来保存父节点和子节点之间的关系一开始写的时候TLE了,先看看错误的写法:class ThroneInheritance: def __init__(self, kingName: st
2020-10-08 12:21:40 116
原创 Leetcode 1478. Allocate Mailboxes (python)
Leetcode 1478. Allocate Mailboxes题目解法:follow up题目解法:首先一定要有这种思想就是他求的是mincost这个值,所以应该往动态规划上去想。而且这个问题的动态规划肯定是二维的,这张基本思想要有。这里的动态规划也是从放置的最后一个mailbox出发来想,当最后一个mailbox放置的位置定了之后,那么最终的cost就是放置最后一个mailbox造成的cost和前面的相应的子问题造成的cost。也就是说我们要求解的是最后一个mailbox覆盖若干个house
2020-10-07 10:53:18 250
原创 Leetcode1610. Maximum Number of Visible Points (python+cpp)
Leetcode 1610. Maximum Number of Visible Points题目解析:错误解法正确解法:题目解析:错误解法这道题简单来说就是雷达扫点的问题。一开始我想入了一种错误思路。就是类似于动雷达的方式然后判断在当前的范围内有多少个点这样。但是问题是对于雷达应该转任意角度都可以,所以遍历这个转的角度并不行class Solution: def visiblePoints(self, points: List[List[int]], angle: int, locati
2020-10-07 10:16:56 451
原创 Leetcode 1609. Even Odd Tree (python)
Leetcode 1609. Even Odd Tree题目解法1:BFS解法2:DFSfollow题目解法1:BFS本质上是个level order traversal再加一些判断即可,BFS版本的解法中需要额外的空间class Solution: def isEvenOddTree(self, root: TreeNode) -> bool: q = collections.deque() q.append(root) levels
2020-10-07 09:38:00 294
原创 Leetcode 351. Android Unlock Patterns (python)
Leetcode 351. Android Unlock Patterns题目解法:backtracking题目解法:backtracking这道题读懂题意挺关键。题目的意思是,从一个位置到另一个位置有两种情况,一种情况是到上下左右或者四个对角相邻的位置,另一种情况通过相邻且已经被访问过的位置到另一个不相邻的位置主要思想就是铜鼓obacktracking来访问所有可能的解法。为啥是backtracking呢,因为对于解法的长度有一个范围,也就是当前一种解法成立的时候,继续扩展下去还有可能形成新的解
2020-10-04 08:04:02 587 1
原创 Leetcode 48. Rotate Image (python)
Leetcode 48. Rotate Image题目解法:follow up1: 逆时针旋转90度follow up 2:旋转180度题目解法:先transpose,然后把每行reverseclass Solution: def rotate(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead.
2020-10-03 08:55:55 426
原创 格式化print括号与字符
题目解法:设计一个counter,遇到{加一,遇到}减一,根据counter值来控制每个位置的缩进def print_string(s): count = 0 curr_s = '' for i in range(len(s)): v = s[i] if v=='{': print(' '*count+'{') count+=1 if ord(v)>=ord('a') and
2020-10-03 01:00:28 102
原创 Leetcode 317. Shortest Distance from All Buildings (python+cpp)
Leetcode 317. Shortest Distance from All Buildings题目解法1:BFS解法2:题目解法1:BFSbrutal force的从每一个空地出发,找与building的最短距离之和class Solution: def shortestDistance(self, grid: List[List[int]]) -> int: def helper(i,j): visited = set()
2020-10-03 00:56:16 1495
原创 Leetcode 31. Next Permutation (python)
Leetcode 31. Next Permutation题目解法题目解法首先两个重要观察,对于一个序列来说,递减的序列一定是所有中最大的,而递增的序列一定是最小的。所以要找到next permutation,我们应该从右遍往左边找,找打一个分界线,分界线右边是完全递减的序列。这样就可以知道,对分界线右边的数字做permutation不可能获得更大的值,也就意味着这个时候的next permutation应该是对对分界线左边的第一个值做改动,具体流程是这样的:从现在序列的尾部往前找,找到第一个
2020-10-03 00:35:45 216
原创 Leetcode 394. Decode String (python)
Leetcode 394. Decode String题目解法1:recursion解法2:stack - one pass题目这道题应该跟basic calculator的题目合起来看,本质上是一样的解法1:recursion这里用了一个trick,函数返回值中包含一个index,这样就可以保证我们check到下一层递归处理到的位置class Solution: def decodeString(self, s: str) -> str: def decode(i
2020-10-02 04:08:11 525 1
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人