![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode
lovefreedom22
BUAA.SCSE
展开
-
Leetcode 301 Python
解题思路:左右括号一样多时,能保证正确,先处理‘)’,当右括号比左括号多时,从左到右依次删除’)’,为保证不重复删除,记录下删除的位置,每次从该位置后面删除。处理左括号的方法同理,只需将字符串倒序后处理即可。class Solution(object): def removeInvalidParentheses(self, s): """ :type s...原创 2018-02-25 21:11:17 · 861 阅读 · 0 评论 -
Leetcode 672 Python
思路:将4种flip作为4种状态,1,2,3,4可知1+2=3,1+3=2,2+3=1,因此共有1,2,3,4,1+4,2+4,3+4,initial这8种状态,且m>=3,n>=3时可满足,其余情况只需要分别判断。class Solution(object): def flipLights(self, n, m): """ :type ...原创 2018-02-13 17:24:13 · 203 阅读 · 0 评论 -
Leetcode 365 Python
解题思路:倒水问题,转换为两个杯子向一个大杯子中倒水、舀水的问题,即mx+ny=d,若z<=x+y且z%d==0,说明存在m、n使得最终剩余水为z。根据贝祖等式,d为x、y的最大公约数。class Solution(object): def canMeasureWater(self, x, y, z): """ :type x: int ...原创 2018-02-13 16:21:16 · 347 阅读 · 0 评论 -
Leetcode 47 Python
解题思路:使用set枚举,使用深度优先搜索,找到所有情况。class Solution(object): def permuteUnique(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ self.ans = []...原创 2018-02-13 12:30:03 · 279 阅读 · 0 评论 -
Leetcode 162 Python
思路:使用二分法,若mid > mid+1 说明0-mid必然有一个peak,否则mid-len(nums)必然有一个peak。class Solution(object): def findPeakElement(self, nums): """ :type nums: List[int] :rtype: int ...原创 2018-02-12 21:29:43 · 353 阅读 · 0 评论 -
Leetcode 73 Python
解题思路:需使用constant-space,因此不能根据matrix大小额外开辟空间。所以使用第0行,第0列来记录。并记录其中是否有0,若有0则最后将0行0列全部置0。class Solution(object): def setZeroes(self, matrix): """ :type matrix: List[List[int]]原创 2018-02-07 10:36:36 · 362 阅读 · 0 评论 -
Leetcode 712 Python
解题思路:动态规划,dp[i][j]表示s1到第i个字符与s2到第j个字符保持相同所需删掉的最少的ascii码的和,若s1[i]==s2[j],则dp[i+1][j+1]与dp[i][j]相同。否则,为删掉s1[i]或删掉s2[j]后,前一个状态加上删去字符的ascii码后的最小值。class Solution(object): def minimumDeleteSum(self, s...原创 2018-02-26 21:22:30 · 279 阅读 · 0 评论 -
Leetcode 188 Python
解题思路:使用动态规划的思想。定义全局收益glob和局部收益local,局部收益即当天已经全部卖出可获得的最大收益。 动态转移方程如下: diff = prices[i] - prices[i-1] local[i][j] = max(local[i-1][j]+diff,glob[i-1][j-1]+max(0,diff)) 此处分两种情况,i-1天时卖了股票和i-1前已经全部卖出。原创 2018-02-06 21:47:26 · 335 阅读 · 0 评论 -
Leetcode 124 Python
解题思路:使用递归的方式,root和左右子树最大路径和,以及左右子树分别的路径,取其中最大值,但return时只能是root加左或右子树的路径。# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# s...原创 2018-02-12 12:39:14 · 671 阅读 · 0 评论 -
Leetcode 75 python
解题思路:0移到最左边,2移到最右边,移动后需继续判断,遇到1则跳过。class Solution(object): def sortColors(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place原创 2018-02-06 17:48:28 · 731 阅读 · 0 评论 -
Leetcode 125 Python
解题思路:判断是否为字母数字,并转为小写。class Solution(object): def isPalindrome(self, s): """ :type s: str :rtype: bool """ c = [] for i in s: if i.isal...原创 2018-02-12 10:40:17 · 315 阅读 · 1 评论 -
Leetcode 650 Python
解题思路:动态规划,dp[0],dp[1] = 0, 大于2时,dp[i]=min(dp[i],dp[j]+i/j) (i%j==0)class Solution(object): def minSteps(self, n): """ :type n: int :rtype: int """ dp = ...原创 2018-02-13 18:32:03 · 226 阅读 · 0 评论 -
Leetcode 591 Python
参考了https://www.cnblogs.com/grandyang/p/7016476.html提供的思路思路:使用栈匹配的思想。 1)检查非首字符时,栈是否为空。 2)检查<![CDATA能否和]]>匹配 3)检查</能否和>匹配,若能匹配,则出栈比较 4)检查<能否和>匹配,若能匹配,则检查规则,符合规则方可进栈。 5)全部遍历完...原创 2018-02-13 21:46:53 · 266 阅读 · 0 评论 -
Leetcode 621 Python
解题思路:将数量最多的task排在首位,以该task的间隔进行插空,计算插空能否插满,不能插满则返回带有间隔的时间数,否则返回任务总数即可。class Solution(object): def leastInterval(self, tasks, n): """ :type tasks: List[str] :type n: int ...原创 2018-02-25 16:43:44 · 361 阅读 · 0 评论 -
Leetcode 517 Python
解题思路:动态规划,当前需要的步数为目前需要移动到前面(从前面移动走)和从当前移动走的最大值,求出所有值后,取最大值。class Solution(object): def findMinMoves(self, machines): """ :type machines: List[int] :rtype: int """...原创 2018-02-25 15:17:36 · 196 阅读 · 0 评论 -
Leetcode 662 Python
解题思路:记录节点位置,节点层次,进而计算每层宽度,找出最大值即可。# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right =...原创 2018-02-24 21:53:23 · 435 阅读 · 0 评论 -
Leetcode 208 Python
解题思路:定义一个新的节点类型,TrieNode,其包含一个标记变量,标记是否可能为最后一个节点,且包含一个大小为26的数组。在Trie中,实现包含Trie节点的多叉树即可。class TrieNode(object): def __init__(self, s): """ Initialize your data structure here. ...原创 2018-02-08 14:31:03 · 505 阅读 · 0 评论 -
Leetcode 458 Python
解题思路:每只猪负责检查一个维度,假设x只猪共能检查n次,则能检查的数量为(n+1)^x个桶(最后一维不需检查)class Solution(object): def poorPigs(self, buckets, minutesToDie, minutesToTest): """ :type buckets: int :type min...原创 2018-02-16 16:38:23 · 225 阅读 · 0 评论 -
Leetcode 4 Python
解题思路:将题目转化为,找两个有序数组中第k小的数。如果nums1[k/2] < nums2[k/2],则说明第k小的数一定不在nums1[0…k/2]中,可将这部分舍弃。实际操作中,需要比较长度较小的数组的长度与k/2的大小。class Solution(object): def findMedianSortedArrays(self, nums1, nums2): ...原创 2018-02-08 11:56:00 · 728 阅读 · 0 评论 -
Leetcode 240 Python
解题思路:从右上角扫描,如果当前点大于target,则向左移动,若当前点小于target,则向下移动。class Solution(object): def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int ...原创 2018-02-15 15:02:20 · 351 阅读 · 0 评论 -
Leetcode 380 Python
思路:每次把最后一个元素移动到被删除的元素的位置,然后删除最后一个元素。使用dicts保存每个元素的位置,交换最后一个元素后,如果位置发生了变化,需要对dicts进行修改。class RandomizedSet(object): def __init__(self): """ Initialize your data structure here. ...原创 2018-02-14 19:31:27 · 403 阅读 · 0 评论 -
Leetcode 239 Python
解题思路:维护一个双向队列,其保持递减顺序,若队列头位置不在窗口中,队列头出队列,当前数小于队列尾或队列为空,则队列尾出队列,进行循环操作后,加入当前项。队列头即使当前窗口中的最大值。class Solution(object): def maxSlidingWindow(self, nums, k): """ :type nums: List[in...原创 2018-02-14 15:08:39 · 514 阅读 · 0 评论 -
Leetcode 146 Python
解题思路:使用双向链表存储key代表的node。每次访问或添加时,都将该key对应的node置于头部。若长度超过最大限度,则删除尾部的节点。class Node(object): def __init__(self, key, value): self.key = key self.val = value self.pre = Non原创 2018-02-07 21:30:08 · 344 阅读 · 0 评论 -
Leetcode 101 Python
思路:判断左子树的左(右)子树及右子树的右(左)子树是否相等# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Non...原创 2018-02-11 22:23:35 · 645 阅读 · 0 评论 -
Leetcode 151 C++
Leetcode 151 C++解题思路:翻转两次。要注意多余的空格。而且必须in-place修改。使用storeindex进行保存当前修改到的位置。当storeindex不为0时,需要在后面加空格。class Solution {public: void reverseWords(string &s) { reverse(s.begin(),s.end());原创 2018-02-06 16:01:39 · 356 阅读 · 0 评论 -
Leetcode 525 python 解题报告
525. Contiguous Array 参考了http://blog.csdn.net/wudidi8800/article/details/60802772 的方法AC代码:class Solution(object): def findMaxLength(self, nums): """ :type nums: List[int]原创 2017-03-09 16:34:54 · 489 阅读 · 0 评论 -
Leetcode 399 python 解题报告
AC代码:class Solution(object): def calcEquation(self, equations, values, queries): """ :type equations: List[List[str]] :type values: List[float] :type queries: L原创 2017-02-28 09:37:51 · 462 阅读 · 0 评论 -
Leetcode 435 python 解题报告
AC代码:class Solution(object): def eraseOverlapIntervals(self, intervals): """ :type intervals: List[Interval] :rtype: int """ if len(intervals) == 0:原创 2017-02-15 10:19:24 · 655 阅读 · 0 评论 -
Leetcode 424 python 解题报告
AC代码:import stringclass Solution(object): def characterReplacement(self, s, k): """ :type s: str :type k: int :rtype: int """ window = 1原创 2017-02-14 21:40:09 · 689 阅读 · 0 评论 -
leetcode 416 python 解题报告
参考了http://blog.csdn.net/mebiuw/article/details/52765840的解题方法AC代码:class Solution(object): def canPartition(self, nums): """ :type nums: List[int] :rtype: bool原创 2017-02-13 17:32:43 · 965 阅读 · 0 评论 -
Leetcode 516 python 解题报告
python代码:class Solution(object): def longestPalindromeSubseq(self, s): """ :type s: str :rtype: int """ dp = [[0 for i in range(len(s))] for i in range(原创 2017-02-20 20:27:16 · 774 阅读 · 0 评论 -
Leetcode 529 python 解题报告
AC代码:class Solution(object): def updateBoard(self, board, click): """ :type board: List[List[str]] :type click: List[int] :rtype: List[List[str]] """原创 2017-02-28 21:21:49 · 344 阅读 · 0 评论 -
Leetcode 494 python 解题报告
AC代码:class Solution(object): def findMode(self, root): """ :type root: TreeNode :rtype: List[int] """ self.ans = [] self.max = 1 if root原创 2017-02-19 20:07:56 · 537 阅读 · 0 评论 -
Leetcode 486 python 解题报告
AC代码:class Solution(object): def PredictTheWinner(self, nums): """ :type nums: List[int] :rtype: bool """ total = sum(nums) sum1 = [[0 for i in原创 2017-02-18 12:07:29 · 973 阅读 · 0 评论 -
Leetcode 459 python 解题报告
AC代码:class Solution(object): def repeatedSubstringPattern(self, str): """ :type str: str :rtype: bool """ for i in range(1,len(str)/2+1): if原创 2017-02-17 10:22:31 · 593 阅读 · 0 评论 -
Leetcode 218 C++
解题思路:参考http://blog.csdn.net/qq508618087/article/details/51311778 利用两个multiset进行解决,将起点、终点分别加入到st中,以负号进行区别,同一高度的起点会排在终点之前。然后,按照顺序处理,如果是起点,则将高度加入height中,如果是终点,则删除掉与之对应起点的高度。在加入起点或删除终点对应的高度时,如果最大的高度发生了变话...原创 2018-02-08 21:20:47 · 316 阅读 · 0 评论 -
Leetcode 212 Python
解题思路:Trie+dfs,先将word存入trie,然后dfs遍历board,如果前缀不在word则退出,如果是word,且ans中没有,则加入ans。注:trie实现时需用map,否则tleclass TrieNode: # Initialize your data structure here. def __init__(self): self.child...原创 2018-02-09 13:06:29 · 609 阅读 · 0 评论 -
Leetcode 297 Python
Leetcode 297 Python解题思路:encode时,使用先序遍历方式存储二叉树,并用“,”隔开。decode时,list为python的引用类型,可以改变其值。# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.v原创 2018-02-06 03:08:43 · 492 阅读 · 0 评论 -
Leetcode 204 Python
解题思路:使用倍数法,寻找素数,每次将素数及其倍数全部置False,然后寻找下一个未被置False的数。python超时,c++ AC。class Solution {public: int countPrimes(int n) { bool *Del = new bool[n]; Del[2] = false; for(int...原创 2018-02-11 10:09:39 · 260 阅读 · 0 评论 -
Leetcode 236 python
Leetcode 236 python解题思路:二叉树节点最低公共祖先,可转化为两链条的最后一个公共节点。难点为如何找到两个节点的路径。# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# s原创 2018-02-05 17:48:22 · 1095 阅读 · 0 评论