自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

caisense的专栏

诗和远方

  • 博客(26)
  • 收藏
  • 关注

原创 103. Binary Tree Zigzag Level Order Traversal

https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/ 还是层序遍历,不同的是要求每层的遍历方向不同 思路:还是层序,设一个变量direct,初始为0,每层反转一次,根据direct决定是否将该层数据倒序# Definition for a binary tree node.#...

2018-07-16 15:09:28 122

原创 102. Binary Tree Level Order Traversal

https://leetcode.com/problems/binary-tree-level-order-traversal/description/ 二叉树层序遍历 思路:经典的队列问题# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.va...

2018-07-15 21:22:25 249

原创 101. Symmetric Tree

https://leetcode.com/problems/symmetric-tree/description/ 判断一棵树是否对称思路1:naive的方法。先层序遍历,检查每一层是否对称。此时还不能说明是对称(为什么?见下图) 这棵树层序遍历是“对称”的,但是实际上不对称的所以还要加一次中序遍历,若中序的结果是轴对称的,则符合。注意,若只中序遍历,即使结果轴对称,也不符合,...

2018-07-15 21:00:34 111

原创 100. Same Tree

https://leetcode.com/problems/same-tree/description/ 判断两棵二叉树是否相同(结构一样,相同位置结点一样) 思路:按相同顺序遍历,用一个全局flag,初始为true。每当两棵树的结点不相等,或者一棵树有结点而另一棵树为空时,flag=false# Definition for a binary tree node.# class Tre...

2018-07-14 23:43:20 97

原创 95. Unique Binary Search Trees II

https://leetcode.com/problems/unique-binary-search-trees-ii/description/ 类似96. Unique Binary Search Trees,不同在于要求所有二叉搜索树形状,而不只是计算种数思路:递归。根据二叉搜索树的性质,在[1…n]中依次选择根i(1 <= i <= n),划分出左([1...i-1])右(...

2018-07-14 21:25:14 329

原创 96. Unique Binary Search Trees

https://leetcode.com/problems/unique-binary-search-trees/description/ 给一个数n,求[1..n]能够成二叉搜索树的数目 思路:见discuss。用dpclass Solution: def numTrees(self, n): """ :type n: int :r...

2018-07-14 18:01:16 121

原创 98. Validate Binary Search Tree

https://leetcode.com/problems/validate-binary-search-tree/description/ 判断一棵树是否是二叉搜索树 思路:中序遍历,将结果记录在data,再遍历data,看是否是升序,是则符合。# Definition for a binary tree node.# class TreeNode:# def __ini...

2018-07-13 21:14:13 102

原创 145. Binary Tree Postorder Traversal

https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 二叉树后序遍历 思路1:递归# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = ...

2018-07-13 19:50:28 137

原创 144. Binary Tree Preorder Traversal

https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 二叉树前序遍历 思路1:递归# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x...

2018-07-12 21:56:19 108

原创 94. Binary Tree Inorder Traversal

https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 二叉树中序遍历。 思路1:最简单的,递归# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val ...

2018-07-12 21:22:33 82

原创 349. Intersection of Two Arrays

https://leetcode.com/problems/intersection-of-two-arrays/description/ 求两个数组交集(不含重复) 思路:很简单,用集合求交集即可class Solution: def intersection(self, nums1, nums2): """ :type nums1: List[i...

2018-07-12 17:26:00 75

原创 350. Intersection of Two Arrays II

https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 求两个数组交集,要求包括重复元素。 思路:相比于,此时不能再用交集来求,因为集合不包含重复元素。对两个数组排序,用两个指针i,j分别从头开始比较,较小的向前移动,直到相等,将相等的元素写入结果,直到有一个数组遍历完。class Solutio...

2018-07-12 17:24:08 208

原创 206. Reverse Linked List

https://leetcode.com/problems/reverse-linked-list/description/ 反转链表 思路:头插法。类似:92. Reverse Linked List II # Definition for singly-linked list.# class ListNode:# def __init__(self, x):# ...

2018-07-12 16:11:12 87

原创 92. Reverse Linked List II

https://leetcode.com/problems/reverse-linked-list-ii/description/ 给一个链表和一个区间m,n,要求反转区间内的链表 思路:头插法。先到达第m个结点,用pre记录前驱,cur表示反转后的链尾,move为cur的后继,表示要移动的结点。每次将move插到pre后面即可。class Solution: def revers...

2018-07-12 16:09:00 91

原创 88. Merge Sorted Array

https://leetcode.com/problems/merge-sorted-array/description/ 合并两个有序数组,结果存放在第一个思路1:正向。两个指针分别指向两个数组,从头开始比较合并,取较小的插入nums1缺点在于若发生插入,需要挪动思路2:反向。依然用两个指针,再加一个指针指向合并区末尾。从尾开始比较,取较大的插入nums1末尾,不会发生插入挪动。...

2018-07-11 18:35:14 85

原创 90. Subsets II

https://leetcode.com/problems/subsets-ii/description/ 求一个有重复的数组的笛卡尔积 类似78. Subsets,不同之处在于数组有重复,要考虑去重,加一行判断即可。此外还要先排序,使重复元素都在一起。class Solution: def subsetsWithDup(self, nums): """ ...

2018-07-10 23:53:54 93

原创 89. Gray Code

https://leetcode.com/problems/gray-code/description/ 思路很简单,G(i) = i^ (i/2) 不知道如何解释class Solution: def grayCode(self, n): """ :type n: int :rtype: List[int] """..

2018-07-09 23:26:11 91

原创 86. Partition List

https://leetcode.com/problems/partition-list/description/ 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。 你应当保留两个分区中每个节点的初始相对位置。 思路:用两条链1,2。遍历原链,val小于x的放入1链,其余的放入2链,最后将2链放到1链后即可。# Definitio...

2018-07-09 22:50:57 105

原创 85. Maximal Rectangle

https://leetcode.com/problems/maximal-rectangle/description/求最大的‘1’矩阵面积 思路:dp,参考discuss,用三个数组height,left,right表示状态,逐行进行迭代。每行的状态根据上一行状态和当前状态来决定。left[j]表示改行j位置矩阵的左边界,同理right[j]表示右边界,height表示高度,因此面积可用...

2018-07-09 17:35:00 144

原创 84. Largest Rectangle in Histogram

https://leetcode.com/problems/largest-rectangle-in-histogram/description/求最大的矩形面基 思路:discuss提供的贪心法, 遍历heights数组,每次将下标i入栈,遇到比栈顶高度小的元素就开始计算最大面积。这样栈中的的高度都是递增的。宽度用i - 1 - sidx计算 数组最后加一个辅助的0,用作哨兵,防止边界...

2018-07-08 21:40:31 91

原创 79. Word Search

https://leetcode.com/problems/word-search/description/ 在一个二维字符矩阵中搜索是否存在给定字符串,要求只能水平或垂直搜索 思路:回溯。对每个位置遍历一遍,分别对(i,j)的上下左右位置递归。第一种写法,对一般的测试用例能通过,但对于特别大的矩阵会报超时。class Solution: def exist(self, boa...

2018-07-06 21:25:25 868

原创 76. Minimum Window Substring

https://leetcode.com/problems/minimum-window-substring/description/** 求串匹配的最小窗口 思路:将t的字符存入字典map中,t中未出现而s中出现的词频设为0。用[begin…end]表示窗口,end先向前移动,用直方图法判断子串是否符合t。若符合,begin再向前压缩窗口。class Solution: def ...

2018-07-06 02:40:39 82

原创 83. Remove Duplicates from Sorted List

https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/删除有序链表中重复的结点,只保留一个。 思路:与82. Remove Duplicates from Sorted List II略有不同,之前是pre指向i的前驱,这里pre和i同步,遇到重复时i向前,pre不动;非重复的话i和pre同时...

2018-07-03 13:25:18 324

原创 82. Remove Duplicates from Sorted List II

https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/删除增序链表中的重复结点 思路:用pre记录前驱,i往后遍历,遇到重复的就继续向前,直到非重复,然后修改pre的后继即可class Solution: def deleteDuplicates(self, head):...

2018-07-03 12:58:26 149

原创 72. Edit Distance

https://leetcode.com/problems/edit-distance/description/ 求两个单词的编辑距离 可以替换、删除和插入 思路:dp。主要想法就是已知word1[0…i-2]转换到word2[0…j-2]的距离,在此基础上求word1[0…i-1]转换到word2[0…j-1]的距离。 分几种情况讨论,word1[i-1]与word2[j-1]相等,或者...

2018-07-01 23:13:32 200

原创 81. Search in Rotated Sorted Array II

https://leetcode.com/problems/search-in-rotated-sorted-array-ii/description/与33. Search in Rotated Sorted Array 不同之处在于有重复,需要排除重复干扰 思路:1) everytime check if targe == nums[mid], if so, we find it....

2018-07-01 19:12:56 101

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除