31. Next Permutation-- 数组处理题
摘要:竟然被这么一道简单题折腾了好久,WA了很多次。 算法: 从后往前找,找到a[i-1] 时,从i 到len 中 最小的 但大于 a[i-1]的数,并且交换。 交换后把i 到 len 进行排序。 code 如下: 一开始下标没处理好,21行少了个break, 只得让24行变成 i+2 ,但这样又涵盖不了
阅读全文
posted @
2018-11-14 07:41
KeepAC
阅读(118)
推荐(0)
527. Word Abbreviation -- 贪心法,排序, Tier Tree -ing
摘要:input: ["like","god","internal","me","internet","interval","incension","intension","face","intrusion"] output: ["l2e","god","internal","me","i6t",&quo
阅读全文
posted @
2018-11-13 13:55
KeepAC
阅读(453)
推荐(0)
411. Minimum Unique Word Abbreviation--320+408 back tracking
摘要:这道题就是320和408两个题目合在一起: https://leetcode.com/problems/valid-word-abbreviation/description/ https://leetcode.com/problems/generalized-abbreviation/descri
阅读全文
posted @
2018-11-12 15:39
KeepAC
阅读(148)
推荐(0)
408. Valid Word Abbreviation --字符串处理
摘要:Given s = "internationalization", abbr = "i12iz4n": Return true.abbr 里数字代表相应的字符数,问字符串是否相等虽然是一个easy 的题但却有两个坑:1. abbr 结尾的地方是数字 例如: s= "internationalization" abbr= "i5a11o1" , 因此 return时得加上cout 来判断 ...
阅读全文
posted @
2018-11-12 13:28
KeepAC
阅读(174)
推荐(0)
320. Generalized Abbreviation-- back tracking and bit manipulation(待续)
摘要:需要注意的是, 如果某个节点已经是数字了,则接下来不能继续放数字 ,例如 "a11" 是不合法的。 先写了如下code ,在 input = "interaction" 时WA了, 而其他短字符串都可以过。 因为 最后一行 “curResult.deleteCharAt(curResult.leng
阅读全文
posted @
2018-11-12 08:56
KeepAC
阅读(177)
推荐(0)
89. Gray Code --迭代 和 back tracking 两种方法
摘要:产生格雷码,格雷码就是前后 两个数在二进制上只有一个bit 的差别 先看看格雷码的规律: n =0, [0] n=1, [0 1] n=2: 00 01 11 10 n=3: 000 001 011 010 110 111 101 100 可以发现规律, 比如 n=3 时相比于 n=2 时, 前面
阅读全文
posted @
2018-11-12 06:04
KeepAC
阅读(199)
推荐(0)
17. Letter Combinations of a Phone Number--back tracking--字符数组
摘要:17 给出一个电话拨号盘,数字2-9代表一些字母,已知数字,求出所有字母的可能 例子 比如 求"23" , 2 >"abc" 3-->"def" 画出递归数其实非常简单: 和77. Combinations 本质上是一样的, 都是求所排列问题,但和数字排列不同的时,每一次需要遍历的数组不是固定的,而
阅读全文
posted @
2018-11-11 08:06
KeepAC
阅读(155)
推荐(0)
401. Binary Watch -- back tracking
摘要:题意: 给你个二进制手表,上面 有两排LED等,第一排为hour, 第二排为minutes。 告诉你表上有n 个LED 灯亮着,输出所有可能的时间。 题解: 把上面 10个 LED 等 抽象成10个数 0~9, 现在亮着 n (n<9) 个灯,表示从 nums 里 取出 n 个数的“排列” int[
阅读全文
posted @
2018-11-10 15:36
KeepAC
阅读(148)
推荐(0)
139/140. Word Break II/377. Combination Sum IV--back tracking +Memoization
摘要:和39. Combination Sum 不同的是,377 可以把不同排列的解认为是不同解,例如 [1,1,2] 和 [2,1,1] 是不同解,并且只需要求出解的个数。 例如 这里拿backtraing 每次把sum += nums[i] 再去掉 上一次的 sum -= num[i] 但不是一个好的
阅读全文
posted @
2018-11-10 14:51
KeepAC
阅读(228)
推荐(0)
784. Letter Case Permutation---back tracking
摘要:因为不熟悉 Java 字符串处理的一些function, 一开始写了一个特别丑陋的code: 用了Character 类里的function 后的code: 优化到了95%
阅读全文
posted @
2018-11-10 10:00
KeepAC
阅读(148)
推荐(0)
78/90 Subsets --back tracking
摘要:78 nums 元素没有重复, 求subsets, 共有 2^n个 90. 有重复元素情况下求组合数, 唯一需要注意的是如何去重:
阅读全文
posted @
2018-11-10 09:28
KeepAC
阅读(148)
推荐(0)
77. Combinations 39/40/216 Combination Sum 1 2 and 3 -- back tracking
摘要:77 从1~n 中产生K个组合, 因为1~n 没有重复元素,所以很简单 39. 前提: 1. 所有数都是positive的 2. 所有数没有duplicated的 并且一个数可以重复选择 40. 1. 条件变成了 数字有重复 , 有重复的处理方法都是先排序。 2. 并且返回结果仍然需要unique
阅读全文
posted @
2018-11-10 03:50
KeepAC
阅读(129)
推荐(0)
46/47 Permutations --back tracking 回溯法
摘要:参考 https://blog.csdn.net/wonner_/article/details/80373871 回溯算法的定义:回溯算法也叫试探法,它是一种系统地搜索问题的解的方法。回溯算法的基本思想是:从一条路往前走,能进则进,不能进则退回来,换一条路再试。 回溯算法实际上一个类似枚举的搜索尝
阅读全文
posted @
2018-11-08 16:32
KeepAC
阅读(212)
推荐(0)
104/110/111/559. Max/Min Depth of Binary/N Tree --二叉树或者N叉树深度
摘要:104. Maximum Depth of Binary Tree 求二叉树最大深度 return its depth = 3. 559. Maximum Depth of N-ary Tree 一开始写成这样, 结果怎么都不对: 因为 这里 maxDepth(child) 根本不会 hit roo
阅读全文
posted @
2018-11-08 13:07
KeepAC
阅读(238)
推荐(0)
105/106/889 Construct Binary Tree from 2 of Preorder/Inorder/Postorder Traversal
摘要:105. Construct Binary Tree from Preorder and Inorder Traversal https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
阅读全文
posted @
2018-11-06 15:16
KeepAC
阅读(134)
推荐(0)
814. Binary Tree Pruning --Tree 的遍历
摘要:https://leetcode.com/problems/binary-tree-pruning/description/ 一颗树中有0和1的节点,要求删除所有可以被删除的0节点,直接看下图就明白了。 分析: 如果是叶子节点为0,那直接删除, 删除后 如果新的叶子节点 也是0 继续删除。。。 一开
阅读全文
posted @
2018-11-06 15:06
KeepAC
阅读(109)
推荐(0)
241. Different Ways to Add Parentheses/282. Expression Add Operators ---Divide and conquer求解表达式的值
摘要:241. https://leetcode.com/problems/different-ways-to-add-parentheses/description/ 282. https://leetcode.com/problems/expression-add-operators/descript
阅读全文
posted @
2018-11-06 04:58
KeepAC
阅读(81)
推荐(0)
224/227/772/150 Basic Calculator 1 2 3 and Evaluate Reverse Polish Notation
摘要:1. https://leetcode.com/problems/basic-calculator/description/ 2. https://leetcode.com/problems/basic-calculator-ii/description/ 3. https://leetcode.c
阅读全文
posted @
2018-11-06 04:01
KeepAC
阅读(114)
推荐(0)
20/678/32/22/856/301/921 Parentheses 括号匹配或者生成题
摘要:20. https://leetcode.com/problems/valid-parentheses/description/ 678. https://leetcode.com/problems/valid-parenthesis-string/description/ 32. https://
阅读全文
posted @
2018-11-04 01:20
KeepAC
阅读(183)
推荐(0)
297/449/428/271/431 Serialize and Deserialize Binary Tree/Strings
摘要:1. 297: https://leetcode.com/articles/serialize-and-deserialize-binary-tree/ 2. 449: https://leetcode.com/problems/serialize-and-deserialize-bst/descr
阅读全文
posted @
2018-10-31 03:56
KeepAC
阅读(170)
推荐(0)