414. Third Maximum Number
摘要:return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
阅读全文
posted @
2019-01-15 03:13
KeepAC
阅读(115)
推荐(0)
280. Wiggle Sort/324. Wiggle Sort II
摘要:280 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... Example: 方法二: 上面的方法是 nlogn解法, 如何寻求一个On 的解
阅读全文
posted @
2019-01-15 03:07
KeepAC
阅读(89)
推荐(0)
36. Valid Sudoku/37. Sudoku Solver - 数独问题-- backtracking 经典
摘要:题意: 经典的递归题, 要求:除了要求 横竖都填满 1~9外, 每个3*3也都要求满足 1~9 36. 数组可以部分填充, 问是否一个有效的 sudoku. 写了个好烧脑的 四重循环来check 3*3 的部分。 重点在于 用数组作为hash 。 然后对于 check 3*3 部分, 其实就是9个小
阅读全文
posted @
2018-11-25 05:27
KeepAC
阅读(279)
推荐(0)
351. Android Unlock Patterns -- back tracking 坑多
摘要:android 解锁,规则比较复杂,其中比较复杂的是 1) 从1 到3, 如果要穿越2, 那么2必须已经走过了 分析: 1,3,7,9 是对称的, 2,4,6,8 也是对称的, 5是单独的, 因此只要从 1,3,7,9 中选 1 ,结果 x4, 从 2,4,6,8 中选2 结果x4。 最后结果就是
阅读全文
posted @
2018-11-22 16:00
KeepAC
阅读(245)
推荐(0)
39. Combination Sum vs 377. Combination Sum IV --- 为何377 可以被 memoization,而39不可以
摘要:这两题 都是给定 Nums =[1 2 3] target = 4, 用nums 求和构建 target , 求出可能结果 不同点: 39 题: 认为 [1,1,2] 和 [2,1,1] 是重复结果 377 认为 [1,1,2] 和 [2,1,1] 是不同的结果。 因此画出两个题目的dfs tree
阅读全文
posted @
2018-11-22 04:40
KeepAC
阅读(232)
推荐(0)
425. Word Squares -- back tracking + trie tree(TLE if not)
摘要:给你一个字符数组,每个单词长度一样,你从中选单词,组成的 二位数组中 横向和纵向 组成的一维数组都一样。 注意1: 单词可以重复被选择 注意2. 字符串数组可能非常的大,有1000 个 分析二: 假设单词当都为5, 第一个单词任意放, 第二个单词首字母 [1][0]位置 会被 第一个单词 [0][1
阅读全文
posted @
2018-11-21 05:12
KeepAC
阅读(188)
推荐(0)
383. Ransom Note/691. Stickers to Spell Word-- String, Map, back tracking-- 未完待续
摘要:383 easy 题,就是建立字母的hash 表 看第一个String 是否能被第二个String 所构建 canConstruct("aa", "aab") -> true 统计 第二个参数中每个字母的频率,可以用一个int[256] 建立hashmap, 然后统计 第一个String 中字母出现
阅读全文
posted @
2018-11-19 14:12
KeepAC
阅读(102)
推荐(0)
290/291 Word Pattern I and II -- 290: map 291 map+backtracking
摘要:290 为一个简单题,但还是有个小坑的。 题意: 给你一个pattern 和 一个用空格split 的的str, 问你两边是否匹配。 code 很简单,一看就是用Map, 去建立 char : string 之间的对应关系, 然而还得用一个 set 去保存已经存放的 string 了,例如: pat
阅读全文
posted @
2018-11-18 05:42
KeepAC
阅读(213)
推荐(0)
51/52. N-Queens -- 待优化
摘要:经典的八皇后问题, queen 可以攻击的范围: 水平,垂直,斜向, 放置 queen 让所有的queens 不在对方的攻击范围内,问有多少种方法或者产生相应的棋盘。 分析: 依次产生每一行的结果, 先在某行某个位置放一个结果,如果不能产生解 则back tracking 到上一行,重新放置。 以
阅读全文
posted @
2018-11-17 14:18
KeepAC
阅读(198)
推荐(0)
211 Add and Search Word - Data structure design--- back tracking, map, set 待续 trie
摘要:题意: 设计个字典查询系统, 有 add 和search 两种操作, add 是加入单词到字典里, search 时 可以用 点号通配符 ".", 点号可以匹配一个字母。 分析: 当search 时为 通配符时, 如果直接用back tracking产生 a-z, 比如 有7个点号, 就得生成 26
阅读全文
posted @
2018-11-17 06:57
KeepAC
阅读(136)
推荐(0)
79. Word Search/212. Word Search II--图的back tracking -- tier tree 待续
摘要:79题, 给你一个二维的board, 只能往上下左右四个方向走,为你是否能找到单词。 又改成了: 之所以写的不好,主要因为,应该把 边界条件和是否已经被访问 判断放在 下一次函数调用里,而不是本次里, 修改后的程序简单很多。 back traking 体现在 visted[i][j] = true
阅读全文
posted @
2018-11-17 04:30
KeepAC
阅读(162)
推荐(0)
357. Count Numbers with Unique Digits-- back tacking 或者数学问题
摘要:Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n. Example: Input: 2 Output: 91 Explanation: The answer shou
阅读全文
posted @
2018-11-16 13:03
KeepAC
阅读(109)
推荐(0)
667. Beautiful Arrangement II-- 类比526 但不能用back tracking
摘要:667 是很坑爹的一个题目,乍一看和 526 如出一辙, 526. Beautiful Arrangement 题意: 构造 [1,n]的排列,让每个 a[index] % index ==0 或者 index %a[index] ==0, 基本和 46 题一样,就是构造排列。 667题意: 给定两
阅读全文
posted @
2018-11-16 12:31
KeepAC
阅读(126)
推荐(0)
526. Beautiful Arrangement-- 和46. Permutations 一样 -back tracking
摘要:给一个数N, 产生 一个数列的排列 [1,2,..n] 这个数列符合如下条件 : 第index 个数满足两个条件的一个 :1. index % a[index] ==0 或者 2. a[index]%index ==0 分析: 和 46 permutations 完全一样, 只是需要在dfs 过程中
阅读全文
posted @
2018-11-16 06:11
KeepAC
阅读(128)
推荐(0)
306. Additive Number/842. Split Array into Fibonacci Sequence -- back tracking
摘要:842. Split Array into Fibonacci Sequence 和306几乎一样的代码,只是需要return 结果,而不是判断是否符合条件。 结果必须得是 Integer ,因此 字符串长度 不会 长于 Integer 长度10. 因此加了 for(int i=1; i<=(s.l
阅读全文
posted @
2018-11-16 05:38
KeepAC
阅读(168)
推荐(0)
293/294 Flip Game I and II -- back tracking ing
摘要:294. 在293基础上问你 starting player 是否一定能赢。 分析: 两个player 成为 play0 和 play1, 存在一条路径,让 play0 做出某个选择, play1 无论如何选择 最终都能导致 play1 fail 掉。
阅读全文
posted @
2018-11-16 04:27
KeepAC
阅读(164)
推荐(0)
267. Palindrome Permutation II --back tracking 以及palindrome 的优化方法ing
摘要:Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ] 然而写了一个类似47的解竟然TLE了, 优化策略1. 统计整个字符串中每个字母的个数,如果奇数个的个数>1 ,则所有的permutation 都不可能符合要求。 做了这个优化后写了如下code,
阅读全文
posted @
2018-11-15 03:24
KeepAC
阅读(139)
推荐(0)
131. Palindrome Partitioning--back tracking 和93. Restore IP Addresses本质一样
摘要:给你一个字符串, 输出他的pattitioning 都是 palindrome 的组合 Input: "aab" Output: [ ["aa","b"], ["a","a","b"] ] 和93 题实际上一样, 给你个字符串长度比较是 3, 你相当于有一个nums = {1,2,3} 的数组,每次
阅读全文
posted @
2018-11-15 03:11
KeepAC
阅读(116)
推荐(0)