File tree 7 files changed +269
-1
lines changed
929.Unique Email Addresses
930.Binary Subarrays With Sum
7 files changed +269
-1
lines changed Original file line number Diff line number Diff line change
1
+ const romanToInt = function ( s ) {
2
+ const map = {
3
+ 'I' : 1 ,
4
+ 'V' : 5 ,
5
+ 'X' : 10 ,
6
+ 'L' : 50 ,
7
+ 'C' : 100 ,
8
+ 'D' : 500 ,
9
+ 'M' : 1000
10
+ } ;
11
+ let sum = 0 ;
12
+ for ( let i = 0 ; i < s . length ; i ++ ) {
13
+ if ( map [ s [ i ] ] < map [ s [ i + 1 ] ] ) {
14
+ sum -= map [ s [ i ] ] ;
15
+ } else {
16
+ sum += map [ s [ i ] ] ;
17
+ }
18
+ }
19
+ return sum ;
20
+ } ;
Original file line number Diff line number Diff line change
1
+ const generate = function ( numRows ) {
2
+ let arr = [ ] ;
3
+ for ( let i = 0 ; i < numRows ; i ++ ) {
4
+ let row = [ ] ;
5
+ row [ 0 ] = 1 ;
6
+ row [ i ] = 1 ;
7
+
8
+ for ( let j = 1 ; j < row . length - 1 ; j ++ ) {
9
+ row [ j ] = arr [ i - 1 ] [ j - 1 ] + arr [ i - 1 ] [ j ] ;
10
+ }
11
+ arr . push ( row ) ;
12
+ }
13
+ return arr ;
14
+ }
Original file line number Diff line number Diff line change @@ -62,4 +62,74 @@ class MinStack {
62
62
* int param_3 = obj.top();
63
63
* int param_4 = obj.getMin();
64
64
*/
65
- ```
65
+ ```
66
+
67
+
68
+ 然后还可以再优化一点点, 就是在辅助栈存最小值的索引, 这样可以避免辅助栈的大量的重复元素的情况.
69
+
70
+ ``` JavaScript
71
+ /**
72
+ * initialize your data structure here.
73
+ */
74
+ const MinStack = function () {
75
+ this .arr = [];
76
+ this .help = [];
77
+ };
78
+
79
+ /**
80
+ * @param {number} x
81
+ * @return {void}
82
+ */
83
+ MinStack .prototype .push = function (x ) {
84
+ this .arr .push (x);
85
+ if (this .help .length === 0 ){
86
+ this .help .push (0 );
87
+ }else {
88
+ let min = this .getMin ();
89
+ if (x < min){
90
+ this .help .push (this .arr .length - 1 );
91
+ }
92
+ }
93
+ };
94
+
95
+ /**
96
+ * @return {void}
97
+ */
98
+ MinStack .prototype .pop = function () {
99
+ if (this .arr .length === 0 ){
100
+ throw new Error (' ???' );
101
+ }
102
+ if (this .arr .length - 1 === this .help [this .help .length - 1 ]){
103
+ this .help .pop ();
104
+ }
105
+ this .arr .pop ();
106
+ };
107
+
108
+ /**
109
+ * @return {number}
110
+ */
111
+ MinStack .prototype .top = function () {
112
+ return this .arr [this .arr .length - 1 ];
113
+ };
114
+
115
+ /**
116
+ * @return {number}
117
+ */
118
+ MinStack .prototype .getMin = function () {
119
+ if (this .arr .length === 0 ){
120
+ throw new Error (" ???" );
121
+ }
122
+ return this .arr [this .help [this .help .length - 1 ]];
123
+ };
124
+
125
+ /**
126
+ * Your MinStack object will be instantiated and called as such:
127
+ * var obj = Object.create(MinStack).createNew()
128
+ * obj.push(x)
129
+ * obj.pop()
130
+ * var param_3 = obj.top()
131
+ * var param_4 = obj.getMin()
132
+ */
133
+
134
+ ```
135
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * initialize your data structure here.
3
+ */
4
+ const MinStack = function ( ) {
5
+ this . arr = [ ] ;
6
+ this . help = [ ] ;
7
+ } ;
8
+
9
+ /**
10
+ * @param {number } x
11
+ * @return {void }
12
+ */
13
+ MinStack . prototype . push = function ( x ) {
14
+ this . arr . push ( x ) ;
15
+ if ( this . help . length === 0 ) {
16
+ this . help . push ( 0 ) ;
17
+ } else {
18
+ let min = this . getMin ( ) ;
19
+ if ( x < min ) {
20
+ this . help . push ( this . arr . length - 1 ) ;
21
+ }
22
+ }
23
+ } ;
24
+
25
+ /**
26
+ * @return {void }
27
+ */
28
+ MinStack . prototype . pop = function ( ) {
29
+ if ( this . arr . length === 0 ) {
30
+ throw new Error ( '???' ) ;
31
+ }
32
+ if ( this . arr . length - 1 === this . help [ this . help . length - 1 ] ) {
33
+ this . help . pop ( ) ;
34
+ }
35
+ this . arr . pop ( ) ;
36
+ } ;
37
+
38
+ /**
39
+ * @return {number }
40
+ */
41
+ MinStack . prototype . top = function ( ) {
42
+ return this . arr [ this . arr . length - 1 ] ;
43
+ } ;
44
+
45
+ /**
46
+ * @return {number }
47
+ */
48
+ MinStack . prototype . getMin = function ( ) {
49
+ if ( this . arr . length === 0 ) {
50
+ throw new Error ( "???" ) ;
51
+ }
52
+ return this . arr [ this . help [ this . help . length - 1 ] ] ;
53
+ } ;
54
+
55
+ /**
56
+ * Your MinStack object will be instantiated and called as such:
57
+ * var obj = Object.create(MinStack).createNew()
58
+ * obj.push(x)
59
+ * obj.pop()
60
+ * var param_3 = obj.top()
61
+ * var param_4 = obj.getMin()
62
+ */
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ */
4
+ const Solution = function ( nums ) {
5
+ this . nums = nums || [ ] ;
6
+ } ;
7
+
8
+ /**
9
+ * Resets the array to its original configuration and return it.
10
+ * @return {number[] }
11
+ */
12
+ Solution . prototype . reset = function ( ) {
13
+ return this . nums ;
14
+ } ;
15
+
16
+ /**
17
+ * Returns a random shuffling of the array.
18
+ * @return {number[] }
19
+ */
20
+ Solution . prototype . shuffle = function ( ) {
21
+ let a = this . nums . slice ( ) ;
22
+ for ( let i = 0 ; i < a . length ; i ++ ) {
23
+ let rand = Math . floor ( Math . random ( ) * ( a . length - i ) ) + i ;
24
+ let tmp = a [ i ] ;
25
+ a [ i ] = a [ rand ] ;
26
+ a [ rand ] = tmp ;
27
+ }
28
+ return a ;
29
+ } ;
30
+
31
+ /**
32
+ * Your Solution object will be instantiated and called as such:
33
+ * var obj = Object.create(Solution).createNew(nums)
34
+ * var param_1 = obj.reset()
35
+ * var param_2 = obj.shuffle()
36
+ */
Original file line number Diff line number Diff line change
1
+ const numUniqueEmails2 = function ( emails ) {
2
+ const emailFilter = function ( str ) {
3
+ let index = str . search ( / @ / ) ;
4
+ let s = str . substring ( 0 , index ) ;
5
+ let s2 = str . substring ( index + 1 , str . length ) ;
6
+ let res = '' ;
7
+ for ( let i = 0 ; i < s . length ; i ++ ) {
8
+ if ( s [ i ] === '+' ) break ;
9
+ if ( s [ i ] === '.' ) continue ;
10
+ res = res + s [ i ] ;
11
+ }
12
+ return res + s2 ;
13
+ }
14
+
15
+ let arr = [ ] ;
16
+ for ( let i = 0 ; i < emails . length ; i ++ ) {
17
+ let t = emailFilter ( emails [ i ] ) ;
18
+ if ( arr . indexOf ( t ) === - 1 ) {
19
+ arr . push ( t ) ;
20
+ }
21
+ }
22
+ return arr . length ;
23
+ } ;
24
+
25
+ const numUniqueEmails = function ( emails ) {
26
+ let arr = emails . map ( str => {
27
+ let index = str . search ( / @ / ) ;
28
+ let s = str . substring ( 0 , index ) ;
29
+ let s2 = str . substring ( index + 1 , str . length ) ;
30
+ let res = '' ;
31
+ for ( let i = 0 ; i < s . length ; i ++ ) {
32
+ if ( s [ i ] === '+' ) break ;
33
+ if ( s [ i ] === '.' ) continue ;
34
+ res = res + s [ i ] ;
35
+ }
36
+ return res + s2 ;
37
+ } ) ;
38
+ let set = new Set ( arr ) ;
39
+ return set . size ;
40
+ }
Original file line number Diff line number Diff line change
1
+ const numSubarrayWithSum = function ( A , S ) {
2
+ let count = 0 ;
3
+ for ( let i = 0 ; i < A . length ; i ++ ) {
4
+ if ( A [ i ] === 1 ) count ++ ;
5
+ }
6
+ if ( S > count ) return 0 ;
7
+ let count2 = 0 ;
8
+ let res = 0 ;
9
+ for ( let i = 0 ; i < A . length ; i ++ ) {
10
+ if ( S > count - count2 ) break ;
11
+ let t = 0 ;
12
+ for ( let j = i ; j < A . length ; j ++ ) {
13
+ t += A [ j ] ;
14
+ if ( t === S ) {
15
+ res ++ ;
16
+ for ( let k = j + 1 ; k < A . length ; k ++ ) {
17
+ if ( A [ k ] === 0 ) res ++ ;
18
+ else break ;
19
+ }
20
+ break ;
21
+ }
22
+ }
23
+ if ( A [ i ] === 1 ) count2 ++ ;
24
+ }
25
+ return res ;
26
+ } ;
You can’t perform that action at this time.
0 commit comments