File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number[][] }
4+ */
5+ var permute = function ( nums ) {
6+ const ans = [ ]
7+ const bkArr = [ ]
8+ const visit = Array . from ( { length : nums . length } ) . fill ( false )
9+ const bk = ( k ) => {
10+ if ( k >= nums . length ) {
11+ const newArr = JSON . parse ( JSON . stringify ( bkArr ) )
12+ ans . push ( newArr )
13+ return
14+ }
15+ for ( let i = 0 ; i < nums . length ; i ++ ) {
16+ if ( ! visit [ i ] ) {
17+ visit [ i ] = true ;
18+ bkArr [ k ] = nums [ i ]
19+ bk ( k + 1 )
20+ visit [ i ] = false ;
21+ }
22+ }
23+ }
24+ bk ( 0 )
25+ return ans
26+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s1
3+ * @param {string } s2
4+ * @return {boolean }
5+ */
6+ var checkInclusion = function ( s1 , s2 ) {
7+
8+ const s1Count = Array ( 26 ) . fill ( 0 ) ;
9+ const s2Count = Array ( 26 ) . fill ( 0 ) ;
10+ const aCode = 'a' . charCodeAt ( 0 ) ;
11+
12+ for ( let i = 0 ; i < s1 . length ; i ++ ) {
13+ s1Count [ s1 . charCodeAt ( i ) - aCode ] ++ ;
14+ s2Count [ s2 . charCodeAt ( i ) - aCode ] ++ ;
15+ }
16+
17+ const matches = ( arr1 , arr2 ) => {
18+ for ( let i = 0 ; i < 26 ; i ++ ) {
19+ if ( arr1 [ i ] !== arr2 [ i ] ) return false ;
20+ }
21+ return true ;
22+ } ;
23+ for ( let i = 0 ; i < s2 . length ; i ++ ) {
24+ if ( matches ( s1Count , s2Count ) ) {
25+ return true ;
26+ }
27+ s2Count [ s2 . charCodeAt ( i + s1 . length ) - aCode ] ++ ;
28+ s2Count [ s2 . charCodeAt ( i ) - aCode ] -- ;
29+ }
30+
31+ return matches ( s1Count , s2Count ) ;
32+ } ;
You can’t perform that action at this time.
0 commit comments