leetcode529. Minesweeper

2020-01-29
阅读 5 分钟
2.2k
You are given a 2D char matrix representing the game board. 'M' represents an unrevealed mine,'E' represents an unrevealed empty square, 'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit ('1' to '8') represents how many mines...

leetcode542. 01 Matrix

2020-01-27
阅读 4 分钟
2.4k
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.

leetcode538. Convert BST to Greater Tree

2020-01-26
阅读 2 分钟
2.2k
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.

547. Friend Circles

2020-01-19
阅读 3 分钟
2.7k
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direc...

leetcode464. Can I Win

2019-11-30
阅读 3 分钟
2.7k
In the "100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.

leetcode472. Concatenated Words

2019-11-01
阅读 3 分钟
2.5k
Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words.A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.

leetcode473. Matchsticks to Square

2019-10-30
阅读 3 分钟
1.3k
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.

leetcode491. Increasing Subsequences

2019-10-14
阅读 2 分钟
1.7k
这里采用深度优先的思路进行解决。先将数组按照从小到大排序,再从左往右遍历数组,每个数字有两种可能,分别是选中到子数组,或者不选中。将所有的结果收纳即可获得最终的结果集。

leetcode365. Water and Jug Problem

2018-11-22
阅读 4 分钟
1.5k
假设现在有两个杯子,每个杯子分别最多可以装x和y升水,假设现在水的供应量是无限的,问是否有可能用这两个杯子共同承装z升水,可以用两个杯子执行的操作如下:

leetcode306. Additive Number

2018-04-01
阅读 2 分钟
3k
这里涉及一个广度优先遍历,首先尝试所有合适的第一个数字和第二个数字,然后在此基础上继续判断后序的字符串是否是一个Additive Number。

leetcode 329. Longest Increasing Path in a Matrix

2018-03-29
阅读 2 分钟
2.1k
这里采用广度优先算法加上缓存的方式来实现。我们可以看到,以一个节点作为开始构成的最长路径长度是确定的。因此我们可以充分利用之前得到的结论来减少重复遍历的次数。

leetcode 301. Remove Invalid Parentheses

2018-03-12
阅读 3 分钟
4.2k
现在有一个字符串包含一些左右括号以及字母。一个合法的字符串是指左括号和右括号必定成对出现。要求得出用最少次数的删除可以得到的所有的合法字符串。

leetcode494. Target Sum

2018-03-05
阅读 3 分钟
2.3k
直观的来说,我们可以将所有的情况都尝试一遍并且将可能构成结果的集合统计下来。就以题目中例子来说,原始的输入为[1,1,1,1,1],目标值为3。那么我们假设先给第一个值设置为正数,则只需要知道[1,1,1,1]组合成目标值2的集合个数即可。通过不断的递归调用,当遍历到数组的尽头时,我们只需要知道当前的目标值是否为0,如...

leetcode130. Surrounded Regions

2017-09-12
阅读 2 分钟
2.5k
这篇题目与leetcode200. Number of Islands思路非常相近,建议毫无思路的同学先参考一下这篇博客。其实这种将区域相连的题目往往都可以使用深度优先遍历或者是Union-Find方法来实现。在这里我就给出深度优先遍历的实现方法,有兴趣的同学可以参考上文的博客来自己实现Union-Find方法。

leetcode200. Number of Islands

2017-09-08
阅读 3 分钟
3k
这道题目从经典的数据结构的角度来说可以使用并查集来进行判断,将每一个海洋看做一个集合合并起来,将相邻的陆地通过并查集连接起来。最后查看并查集中剩余下的集合数。这里使用一个新的二维数组来表示对应地图上的元素属于哪个并查集。在合并的时候先进行判断,如果二者为已经相连的陆地,则无需合并,否则将新的二维...

leetcode126. Word Ladder II

2017-08-14
阅读 4 分钟
3.5k
u may assume beginWord and endWord are non-empty and are not the same.

leetcode124. Binary Tree Maximum Path Sum

2017-08-13
阅读 2 分钟
2k
在这里和其它题目不一样的是,并非从根节点到叶节点的一条路径,而是从任意一个节点到任意一个节点的路径。其实在这里我们通过递归的方法可以发现以下几种场景:

leetcode114. Flatten Binary Tree to Linked List

2017-08-04
阅读 2 分钟
1.9k
题目要求 {代码...} 将一棵二叉树展开形成一棵链表形状的树。本质上是将该树转变成先序遍历后的样子。 思路一:非递归 如果我们从图形的角度来说,每一次都将当前节点的右子树拼接到左子节点的右子树下,再将左节点替换原来的右节点。所以这个例题一步步的操作如下: {代码...} 代码如下: {代码...} 思路二:递归 其实...

leetcode98. Validate Binary Search Tree

2017-07-20
阅读 2 分钟
1.5k
我们可以发现,如果已知当前节点的值val以及取值的上下限upper,lower,那么左子节点的取值范围就是(lower, val),右子节点的取值范围就是(val, upper)。由此出发递归判断,时间复杂度为O(n)因为每个节点只需要遍历一次。

leetcode98. Validate Binary Search Tree

2017-07-20
阅读 2 分钟
1.6k
如果了解中序遍历的同学可以知道,一颗二叉查找树的中序遍历结果应当是一个数值有小到大递增的数组。正因为中序遍历是指先遍历左子树,接着遍历中间节点,再遍历右子树,恰好和查找树的值递增顺序相同。因此在这里我们采用栈的方式实现中序遍历,通过研究中序遍历是否递增来判断二叉查找树是否符合规则。代码如下:

leetcode98. Validate Binary Search Tree

2017-07-20
阅读 2 分钟
1.3k
如果了解中序遍历的同学可以知道,一颗二叉查找树的中序遍历结果应当是一个数值有小到大递增的数组。正因为中序遍历是指先遍历左子树,接着遍历中间节点,再遍历右子树,恰好和查找树的值递增顺序相同。因此在这里我们采用栈的方式实现中序遍历,通过研究中序遍历是否递增来判断二叉查找树是否符合规则。代码如下: