随笔分类 -  LeetCode

摘要:http://www.lintcode.com/zh-cn/problem/next-permutation-ii/# 原题 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列。 如果没有下一个排列,则输出字典序最小的序列。 样例 左边是原始排列,右边是对应的下一个排列。 1 阅读全文
posted @ 2017-03-31 11:47 小丑进场 阅读(267) 评论(0) 推荐(0)
摘要:http://www.lintcode.com/zh-cn/problem/permutation-sequence/# 原题 给定 n 和 k,求123..n组成的排列中的第 k 个排列。 注意事项 1 ≤ n ≤ 9 给定 n 和 k,求123..n组成的排列中的第 k 个排列。 注意事项 1 阅读全文
posted @ 2017-03-28 15:09 小丑进场 阅读(535) 评论(0) 推荐(0)
摘要:原题 Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have the following permutations: 解题思路 递归:递归的方法,创建一个vi 阅读全文
posted @ 2017-03-28 11:11 小丑进场 阅读(156) 评论(0) 推荐(0)
摘要:原题 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in 阅读全文
posted @ 2017-03-28 09:12 小丑进场 阅读(133) 评论(0) 推荐(0)
摘要:原题 给定一个含不同整数的集合,返回其所有的子集 如果 S = [1,2,3],有如下的解: 子集中的元素排列必须是非降序的,解集必须不包含重复的子集 解题思路 Backtracking, DFS 数组要排序,接着一层一层的递归,每一层列表的元素个数加一 完整代码 阅读全文
posted @ 2017-03-27 11:33 小丑进场 阅读(143) 评论(0) 推荐(0)
摘要:http://www.lintcode.com/zh-cn/problem/generate-parentheses/ 原题 给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。 样例 给定 n = 3, 可生成的组合如下: "((()))", "(()())", "(())( 阅读全文
posted @ 2017-03-27 09:57 小丑进场 阅读(174) 评论(0) 推荐(0)
摘要:原题 Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 解题思路 代码实现 阅读全文
posted @ 2017-03-27 09:36 小丑进场 阅读(218) 评论(0) 推荐(0)
摘要:题目 There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you hav 阅读全文
posted @ 2017-03-26 09:21 小丑进场 阅读(308) 评论(0) 推荐(0)
摘要:原题 设计一个包含下面两个操作的数据结构:addWord(word), search(word)addWord(word)会在数据结构中添加一个单词。而search(word)则支持普通的单词查询或是只包含. 和a-z的简易正则表达式的查询。一个 . 可以代表一个任何的字母。 样例 解题思路 本题跟 阅读全文
posted @ 2017-03-21 14:14 小丑进场 阅读(153) 评论(0) 推荐(0)
摘要:原题 Implement a trie with insert, search, and startsWith methods. 实现一个 Trie,包含 insert, search, 和 startsWith 这三个方法。 样例 解题思路 首先,定义一个trie树节点,包含当前的char和isW 阅读全文
posted @ 2017-03-21 10:36 小丑进场 阅读(283) 评论(0) 推荐(0)
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题思路: 1. 找到数组的中间节点将其置为根节点 2. 左边的即为左子树 3. 右边的即为右子树 4. 阅读全文
posted @ 2017-03-18 11:20 小丑进场 阅读(125) 评论(0) 推荐(0)
摘要:Given a binary search tree, write a function kthSmallest to find thekth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's 阅读全文
posted @ 2017-03-18 10:39 小丑进场 阅读(157) 评论(0) 推荐(0)