File tree Expand file tree Collapse file tree 2 files changed +91
-0
lines changed Expand file tree Collapse file tree 2 files changed +91
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ */
5+
6+ /**
7+ * Runtime: 63ms, Memory: 51.68MB
8+ * Time complexity: O(nlogn)
9+ * Space complexity: O(nlogn)
10+ *
11+ */
12+
13+ var missingNumber = function ( nums ) {
14+ const n = nums . length ;
15+ nums . sort ( ( a , b ) => a - b ) ;
16+
17+ if ( ! nums . includes ( 0 ) ) {
18+ return 0 ;
19+ }
20+ for ( let i = 0 ; i < n ; i ++ ) {
21+ if ( nums [ i + 1 ] - nums [ i ] !== 1 ) {
22+ return nums [ i ] + 1 ;
23+ }
24+ }
25+ return nums [ - 1 ] ;
26+ } ;
27+
28+ /**
29+ * NOTE
30+ * if use 'sort()' -> O(nlogn)
31+ * if you solve this problem without using sort(), can use sum of nums
32+ */
33+
34+ var missingNumber = function ( nums ) {
35+ const sumOfNums = nums . reduce ( ( num , total ) => num + total , 0 ) ;
36+
37+ const n = nums . length ;
38+ const expectedSum = ( n * ( n + 1 ) ) / 2 ;
39+
40+ if ( expectedSum === sumOfNums ) {
41+ return 0 ;
42+ } else {
43+ return expectedSum - sumOfNums ;
44+ }
45+ } ;
46+
47+ /**
48+ * NOTE
49+ * or you can subtract while adding
50+ */
51+
52+ var missingNumber = function ( nums ) {
53+ let target = 0 ;
54+ for ( let i = 0 ; i <= nums . length ; i ++ ) {
55+ target += i ;
56+
57+ if ( i < nums . length ) {
58+ target -= nums [ i ] ;
59+ }
60+ }
61+ return target ;
62+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {boolean }
4+ */
5+
6+ /**
7+ * Runtime: 66ms, Memory: 54.75MB
8+ * Time complexity: O(s.length)
9+ * Space complexity: O(s.length)
10+ *
11+ */
12+
13+ var isPalindrome = function ( s ) {
14+ let trimmed = s . toLowerCase ( ) ;
15+ let answer = [ ] ;
16+ let checkAlphabet = / [ a - z A - Z ] / ;
17+ let checkNum = / [ 0 - 9 ] / ;
18+
19+ for ( let alpha of trimmed ) {
20+ if ( checkAlphabet . test ( alpha ) || checkNum . test ( alpha ) ) {
21+ answer . push ( alpha ) ;
22+ }
23+ }
24+
25+ if ( answer . join ( "" ) === answer . reverse ( ) . join ( "" ) ) {
26+ return true ;
27+ }
28+ return false ;
29+ } ;
You can’t perform that action at this time.
0 commit comments