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- 学习笔记
1+ # 第九周学习笔记
2+ ## 高级动态规划
3+ ### 最长子序列 Longest common sequence
4+ dp[ i] [ j ] = dp[ i-1] [ j-1 ] (if s1[ i-1] == s[ j-1] )
5+ else dp[ i] [ j ] = max(dp[ i-1] [ j ] , dp[ i] [ j-1 ] )
6+ ### 最长子串 Longest common substring
7+ dp[ i] [ j ] = dp[ i-1] [ j-1 ] + 1 (if s1[ si[ i-1] == s2[ j-1]] )
8+ else dp[ i] [ j ] = 0
9+ ### 编辑距离
10+ * 如果word1[ i] 与word2[ j] 相同,显然dp[ i] [ j ] = dp[ i-1] [ j-1 ]
11+ * 如果word1[ i] 与word2[ j] 不同,那么dp[ i] [ j ] 可以通过
12+ 1 . 在dp[ i-1] [ j-1 ] 的基础上做replace操作达到目的
13+ 2 . 在dp[ i-1] [ j ] 的基础上做insert操作达到目的
14+ 3 . 在dp[ i] [ j-1 ] 的基础上做delete操作达到目的
15+ 取三者最小情况即可
16+
17+ ## 字符串匹配算法
18+ 1 . 暴力法 brute force
19+ 2 . Rabin-Karp算法
20+ 1 . 假设子串的长度为M(pat), 目标字符串的长度为N(txt)
21+ 2 . 计算子串的hash值hash_pat
22+ 3 . 计算目标字符串txt中每个长度为M的子串的hash值(共需要计算N-M+1次)
23+ 4 . 比较hash值: 如果hash值不同,字符串必然不匹配;如果hash值相同,则还需要通过朴素算法再次判断
24+ 3 . KMP算法
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {number }
4+ */
5+ var numDecodings = function ( s ) {
6+ if ( s . length <= 0 || s [ 0 ] === '0' ) {
7+ return 0
8+ }
9+ let pre = 1 , curr = 1
10+ for ( let i = 1 ; i < s . length ; i ++ ) {
11+ const temp = curr
12+ if ( s [ i ] === '0' ) {
13+ if ( s [ i - 1 ] === '1' || s [ i - 1 ] === '2' ) {
14+ curr = pre
15+ } else {
16+ return 0
17+ }
18+ } else if ( s [ i - 1 ] === '1' || ( s [ i - 1 ] === '2' && s [ i ] >= '1' && s [ i ] <= '6' ) ) {
19+ curr = curr + pre
20+ }
21+ pre = temp
22+ }
23+
24+ return curr
25+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 暴力法
3+ * @param {string } s
4+ * @param {string } p
5+ * @return {number[] }
6+ */
7+ var findAnagrams = function ( s , p ) {
8+ const isAnagrams = ( word1 , word2 ) => {
9+ if ( word1 . length !== word2 . length ) {
10+ return false
11+ }
12+ const res = new Array ( 26 ) . fill ( 0 )
13+ const _start = 'a' . charCodeAt ( 0 )
14+ for ( let i = 0 ; i < word1 . length ; i ++ ) {
15+ res [ word1 . charCodeAt ( i ) - _start ] ++
16+ res [ word2 . charCodeAt ( i ) - _start ] --
17+ }
18+ for ( let i = 0 ; i < res . length ; i ++ ) {
19+ if ( res [ i ] !== 0 ) {
20+ return false
21+ }
22+ }
23+ return true
24+ }
25+ const res = [ ]
26+ const n = p . length
27+ for ( let j = 0 ; j < s . length - n + 1 ; j ++ ) {
28+ if ( isAnagrams ( s . substring ( j , j + n ) , p ) ) {
29+ res . push ( j )
30+ }
31+ }
32+ return res
33+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ */
5+ var lengthOfLIS = function ( nums ) {
6+ if ( nums . length <= 0 ) {
7+ return 0
8+ }
9+ const dp = [ ]
10+ dp [ 0 ] = 1 ;
11+ let max = 1 ;
12+ for ( let i = 1 ; i < nums . length ; i ++ ) {
13+ let _max = 0 ;
14+ for ( let j = 0 ; j <= i ; j ++ ) {
15+ if ( nums [ i ] > nums [ j ] ) {
16+ _max = Math . max ( _max , dp [ j ] )
17+ }
18+ }
19+ dp [ i ] = _max + 1 ;
20+ max = Math . max ( max , dp [ i ] )
21+ }
22+ return max
23+ } ;
You can’t perform that action at this time.
0 commit comments