File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * 使用图论,求最短路径,通过队列使用BFS广度优先遍历
3+ * @param {number } n
4+ * @return {number }
5+ */
6+ var numSquares = function ( n ) {
7+ if ( n < 1 ) { return ; }
8+ const queue = [ [ n , 0 ] ] ; // [[完全平方差, 步数]]
9+ const set = new Set ( ) ; // 保存访问过的节点
10+ set . add ( n ) ;
11+ while ( queue . length != 0 ) {
12+ const el = queue . shift ( ) ;
13+ const num = el [ 0 ] ;
14+ const step = el [ 1 ] ;
15+
16+ if ( num == 0 ) { // 找到最短路径
17+ return step ;
18+ }
19+
20+ for ( let i = 1 ; ; i ++ ) {
21+ const nn = num - i * i ;
22+ if ( nn < 0 ) { // num - i*i >= 0
23+ break ;
24+ }
25+ if ( ! set . has ( nn ) ) {
26+ queue . push ( [ nn , step + 1 ] ) ; // 保存节点和步数,节点是完全平方差
27+ set . add ( nn ) ;
28+ }
29+ }
30+ }
31+ return ;
32+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 455. 分发饼干
3+ * https://leetcode-cn.com/problems/assign-cookies/
4+ * 贪心算法,greedy
5+ * T(n) = O(nlogn)
6+ * @param {number[] } g
7+ * @param {number[] } s
8+ * @return {number }
9+ */
10+ var findContentChildren = function ( g , s ) {
11+ // 将小孩的饼干从大到小排序
12+ g = g . sort ( ( a , b ) => b - a ) ;
13+ s = s . sort ( ( a , b ) => b - a ) ;
14+
15+ let si = 0 , gi = 0 , res = 0 ;
16+ while ( gi < g . length && si < s . length ) {
17+ if ( s [ si ] >= g [ gi ] ) {
18+ res ++ ;
19+ si ++ ;
20+ gi ++ ;
21+ } else {
22+ gi ++ ;
23+ }
24+ }
25+ return res ;
26+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 51. N皇后
3+ * https://leetcode-cn.com/problems/n-queens/
4+ * 递归,回溯
5+ * @param {number } n
6+ * @return {string[][] }
7+ */
8+ var solveNQueens = function ( n ) {
9+ const col = new Array ( n ) . fill ( false ) ;
10+ const dia1 = new Array ( 2 * n - 1 ) . fill ( false ) ;
11+ const dia2 = new Array ( 2 * n - 1 ) . fill ( false ) ;
12+
13+ // 尝试在一个 n 皇后问题中,摆放第 index 行的皇后位置
14+ function putQueen ( n , index , row ) {
15+ if ( index == n ) {
16+ res . push ( generateBoard ( n , row ) ) ;
17+ }
18+
19+ for ( let i = 0 ; i < n ; i ++ ) {
20+ // 尝试将第index行的皇后放到第i列
21+ if ( ! col [ i ] && ! dia1 [ index + 1 ] && ! dia2 [ index - n - 1 ] ) {
22+ // 开始递归
23+ row . push ( i ) ;
24+ col [ i ] = true ;
25+ dia1 [ index + i ] = true ;
26+ dia2 [ index - i + n - 1 ] = true ;
27+ putQueen ( n , index + 1 , row ) ;
28+
29+ // 开始回溯
30+ col [ i ] = false ;
31+ dia1 [ index + i ] = false ;
32+ dia2 [ index - i + n - 1 ] = false ;
33+ row . pop ( ) ;
34+ }
35+ }
36+ return ;
37+ }
38+
39+ function generateBoard ( n , row ) {
40+ // 初始化 n 个 string 每个 string n 个 '.'
41+ const board = new Array ( n ) . fill ( new Array ( n ) . fill ( '.' ) . join ( '' ) ) ;
42+ for ( let i = 0 ; i < n ; i ++ ) {
43+ const s = board [ i ] . split ( '' ) ;
44+ s [ row [ i ] ] = 'Q' ;
45+ board [ i ] = s . join ( '' ) ;
46+ }
47+ return board ;
48+ }
49+
50+ const res = [ ] ;
51+ const row = [ ] ;
52+ putQueen ( n , 0 , row ) ;
53+
54+ return res ;
55+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 784. 字母大小写全排列
3+ * https://leetcode-cn.com/problems/letter-case-permutation/
4+ * 深度优先DFS 递归 https://user-images.githubusercontent.com/49065208/56465164-f0a10800-642a-11e9-81a0-5894bb030e6f.png
5+ * dfs(arr, i):
6+ * if i == str.length: res.push(str.json(''))
7+ * dfs(arr, i + 1)
8+ * if arr[i] is letter:
9+ * toggleCase(arr[i])
10+ * dfs(arr, i + 1)
11+ * toggleCase(arr[i]) // toggle back case
12+ *
13+ * @param {string } S
14+ * @return {string[] }
15+ */
16+ var letterCasePermutation = function ( S ) {
17+ const result = [ ] ;
18+ function toggleCase ( s ) {
19+ if ( / [ A - Z ] / . test ( s ) ) {
20+ return s . toLowerCase ( ) ;
21+ } else {
22+ return s . toUpperCase ( ) ;
23+ }
24+ }
25+
26+ function dfs ( arr , i , res ) {
27+ if ( i == arr . length ) {
28+ res . push ( arr . join ( '' ) ) ;
29+ return ;
30+ }
31+ dfs ( arr , i + 1 , res ) ;
32+ if ( / \d / . test ( arr [ i ] ) ) { return ; } // is number
33+ arr [ i ] = toggleCase ( arr [ i ] ) ; // transform to upper/lower case
34+ dfs ( arr , i + 1 , res ) ;
35+ arr [ i ] = toggleCase ( arr [ i ] ) ; // transform back to upper/lower case
36+ }
37+
38+ dfs ( S . split ( '' ) , 0 , result ) ;
39+
40+ return result ;
41+ } ;
Original file line number Diff line number Diff line change 1- # 学习笔记
1+ # 学习笔记
2+
3+ - 动态规划
4+ - 自底向上
5+ - 通过求子问题的最优解, 可以获得原问题的最优解
6+ - 有重叠子问题 + 最优子结构,还必须记忆搜索,即可用动态规划
7+
8+ ## 常用数据结构的小结及 ADT API
9+ - Trie 字典树, 每个节点都是 map, 根节点为空
10+ - add(val)
11+ - isWord(val)
12+ - contains(val)
13+ - isPrefix(val)
14+
15+ ## 其他
16+ - JS 初始化二维数组 ` Array(m).fill().map(() => Array(n).fill(-1)) `
17+
18+ # 书本笔记
19+
20+ ![ 14] ( https://user-images.githubusercontent.com/49065208/57494782-8084f400-72fd-11e9-8ad1-49d08aef2b01.png )
21+
22+ ![ 12] ( https://user-images.githubusercontent.com/49065208/57494772-719e4180-72fd-11e9-818a-7c07fb45df5e.jpg )
23+
24+ ![ 13] ( https://user-images.githubusercontent.com/49065208/57494774-7236d800-72fd-11e9-9084-8133a8f807f4.jpg )
You can’t perform that action at this time.
0 commit comments