File tree 3 files changed +114
-0
lines changed
3 files changed +114
-0
lines changed Original file line number Diff line number Diff line change 1
1
# typescript-algorithms
2
+
2
3
some datastructs and algorithms written by ts.
Original file line number Diff line number Diff line change
1
+ // 0 1 0 1 0 1 0 1
2
+ // 64 16 4 1
3
+ // -----------------
4
+ // 1 0 0 0 0 0 0 0
5
+ // 0 1 1 1 1 1 1 1
6
+ // &----------------
7
+ // 0 0 0 0 0 0 0 0
8
+ export default function isPowerOfFour ( num : number ) : boolean {
9
+ return (
10
+ num > 0 && // is positive.
11
+ ( num & ( num - 1 ) ) === 0 && // is power of 2.
12
+ ( num & 0x55555555 ) === num // is power of 4.
13
+ ) ;
14
+ /**
15
+ return (
16
+ (num > 0 && (num ^ 0x1) === 0) ||
17
+ (num ^ 0x4) === 0 ||
18
+ (num ^ 0x10) === 0 ||
19
+ (num ^ 0x40) === 0 ||
20
+ (num ^ 0x100) === 0 ||
21
+ (num ^ 0x400) === 0 ||
22
+ (num ^ 0x1000) === 0 ||
23
+ (num ^ 0x4000) === 0 ||
24
+ (num ^ 0x10000) === 0 ||
25
+ (num ^ 0x40000) === 0 ||
26
+ (num ^ 0x100000) === 0 ||
27
+ (num ^ 0x400000) === 0 ||
28
+ (num ^ 0x1000000) === 0 ||
29
+ (num ^ 0x4000000) === 0 ||
30
+ (num ^ 0x10000000) === 0 ||
31
+ (num ^ 0x40000000) === 0
32
+ );
33
+ */
34
+ }
Original file line number Diff line number Diff line change
1
+ export default function findDiagonalOrder ( matrix : number [ ] [ ] ) : number [ ] {
2
+ let n = matrix . length ;
3
+ if ( ! n ) return [ ] ;
4
+ let m = matrix [ 0 ] . length ;
5
+ let turns = 2 * n - 1 ;
6
+ let res = [ ] ;
7
+ let forward = true ;
8
+ let x = 0 ;
9
+ let y = 0 ;
10
+ res . push ( matrix [ 0 ] [ 0 ] ) ;
11
+ for ( let i = 2 ; i < turns ; i ++ ) {
12
+ let overHalf = i > Math . ceil ( turns / 2 ) ;
13
+ x = forward && ! overHalf ? x + 1 : x ;
14
+ y = forward && ! overHalf ? y : y + 1 ;
15
+ forward = ! forward ;
16
+ res . push ( matrix [ y ] [ x ] ) ;
17
+ if ( forward ) {
18
+ while ( y > 0 && x < n - 1 ) {
19
+ x += 1 ;
20
+ y -= 1 ;
21
+ console . log ( [ y , x ] ) ;
22
+ res . push ( matrix [ y ] [ x ] ) ;
23
+ }
24
+ } else {
25
+ while ( x > 0 && y < n - 1 ) {
26
+ x -= 1 ;
27
+ y += 1 ;
28
+ console . log ( [ y , x ] ) ;
29
+ res . push ( matrix [ y ] [ x ] ) ;
30
+ }
31
+ }
32
+ }
33
+ res . push ( matrix [ n - 1 ] [ n - 1 ] ) ;
34
+
35
+ return res ;
36
+ }
37
+
38
+ function findDiagonalOrder2 ( matrix : number [ ] [ ] ) : number [ ] {
39
+ let n = matrix . length ;
40
+ let idxes = new Array ( n ) . fill ( 0 ) ;
41
+ let res = [ ] ;
42
+ let m = matrix [ 0 ] . length ;
43
+ let edge = Math . max ( m , n ) ;
44
+ let levels = 2 * edge - 1 ;
45
+ let towns = 1 ;
46
+ let overHalf = false ;
47
+ for ( let i = 0 ; i < levels ; i ++ ) {
48
+ let idx = 0 ;
49
+ overHalf = i >= Math . ceil ( levels / 2 ) ;
50
+ if ( ! overHalf ) {
51
+ while ( idx < towns && idx < n ) {
52
+ res . push ( matrix [ idx ] [ idxes [ idx ] ] ) ;
53
+ idxes [ idx ] ++ ;
54
+ idx ++ ;
55
+ }
56
+ towns ++ ;
57
+ } else {
58
+ while ( idx < towns ) {
59
+ if ( edge - idx > n ) {
60
+ idx ++ ;
61
+ continue ;
62
+ }
63
+ res . push ( matrix [ edge - idx ] [ idxes [ edge - idx ] ] ) ;
64
+ idxes [ edge - idx ] ++ ;
65
+ idx ++ ;
66
+ }
67
+ towns -- ;
68
+ }
69
+ }
70
+ return res ;
71
+ }
72
+
73
+ let res = findDiagonalOrder2 ( [
74
+ [ 1 , 2 , 3 ] ,
75
+ [ 4 , 5 , 6 ] ,
76
+ [ 7 , 8 , 9 ] ,
77
+ ] ) ;
78
+
79
+ console . log ( res ) ;
You can’t perform that action at this time.
0 commit comments