File tree 3 files changed +113
-1
lines changed
3 files changed +113
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
This is the solution collection of my LeetCode problems, most of them are programmed in JavaScript.
4
4
5
- Progress: 12 /
5
+ Progress: 14 /
6
6
7
7
| ID | Title | Solution | Difficulty |
8
8
| ---| ----- | -------- | ---------- |
9
9
| 1| [ Two Sum] ( https://leetcode.com/problems/two-sum/ ) | [ JavaScript] ( ./src/two-sum/res.js ) | Easy|
10
10
| 2| [ Add Two Numbers] ( https://leetcode.com/problems/add-two-numbers/ ) | [ JavaScript] ( ./src/add-two-numbers/res.js ) | Medium|
11
11
| 66| [ Plus One] ( https://leetcode.com/problems/plus-one/ ) | [ JavaScript] ( ./src/plus-one/res.js ) | Easy|
12
+ | 69| [ Sqrt(x)] ( https://leetcode.com/problems/sqrtx/ ) | [ JavaScript] ( ./src/sqrtx/res.js ) | Easy|
12
13
| 175| [ Combine Two Tables] ( https://leetcode.com/problems/combine-two-tables/ ) | [ SQL] ( ./src/combine-two-tables/res.txt ) | Easy|
13
14
| 176| [ Second Highest Salary] ( https://leetcode.com/problems/second-highest-salary/ ) | [ SQL] ( ./src/second-highest-salary/res.txt ) | Easy|
14
15
| 177| [ Nth Highest Salary] ( https://leetcode.com/problems/nth-highest-salary/ ) | [ SQL] ( ./src/nth-highest-salary/res.txt ) | Medium|
@@ -17,4 +18,5 @@ Progress: 12/
17
18
| 184| [ Department Highest Salary] ( https://leetcode.com/problems/department-highest-salary/ ) | [ SQL] ( ./src/department-highest-salary/res.txt ) | Medium|
18
19
| 197| [ Rising Temperature] ( https://leetcode.com/problems/rising-temperature/ ) | [ SQL] ( ./src/rising-temperature/res.txt ) | Easy|
19
20
| 342| [ Power of Four] ( https://leetcode.com/problems/power-of-four/ ) | [ JavaScript] ( ./src/power-of-four/res.js ) | Easy|
21
+ | 404| [ Sum of Left Leaves] ( https://leetcode.com/problems/sum-of-left-leaves/ ) | [ JavaScript] ( ./src/sum-of-left-leaves/res.js ) | Easy|
20
22
| 434| [ Number of Segments in a String] ( https://leetcode.com/problems/number-of-segments-in-a-string/ ) | [ JavaScript] ( ./src/number-of-segments-in-a-string/res.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * res.js
3
+ * @authors Joe Jiang (hijiangtao@gmail.com)
4
+ * @date 2017-02-19 10:53:04
5
+ * @version $Id$
6
+ */
7
+
8
+ /**
9
+ * @param {number } x
10
+ * @return {number }
11
+ */
12
+ let mySqrt = function ( x ) {
13
+ if ( x === 1 || x === 0 ) {
14
+ return x ;
15
+ } else if ( x < 0 ) {
16
+ return 0 ;
17
+ }
18
+
19
+ let i = 1 ;
20
+ for ( ; ; i += 2 ) {
21
+ if ( i * ( i + 1 ) >= x ) {
22
+ break ;
23
+ }
24
+ }
25
+
26
+ for ( ; ; i -- ) {
27
+ if ( i * i <= x ) {
28
+ return i ;
29
+ }
30
+ }
31
+ } ;
32
+
33
+ // Another solution
34
+ let mySqrt = function ( x ) {
35
+ if ( x === 1 || x === 0 ) {
36
+ return x ;
37
+ } else if ( x < 0 ) {
38
+ return 0 ;
39
+ }
40
+
41
+ let min = 1 , max = x + 1 ;
42
+ while ( true ) {
43
+ let mid = min + Number . parseInt ( ( max - min ) / 2 ) ;
44
+
45
+ if ( mid * mid > x ) {
46
+ max = mid - 1 ;
47
+ } else {
48
+ if ( ( mid + 1 ) * ( mid + 1 ) > x ) {
49
+ return mid ;
50
+ } else {
51
+ min = mid + 1 ;
52
+ }
53
+ }
54
+ }
55
+ } ;
Original file line number Diff line number Diff line change
1
+ // Find the sum of all left leaves in a given binary tree.
2
+
3
+ // Example:
4
+
5
+ // 3
6
+ // / \
7
+ // 9 20
8
+ // / \
9
+ // 15 7
10
+
11
+ // There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
12
+
13
+ /**
14
+ * res.js
15
+ * @authors Joe Jiang (hijiangtao@gmail.com)
16
+ * @date 2017-02-19 10:13:41
17
+ * @version $Id$
18
+ */
19
+
20
+ /**
21
+ * Definition for a binary tree node.
22
+ * function TreeNode(val) {
23
+ * this.val = val;
24
+ * this.left = this.right = null;
25
+ * }
26
+ */
27
+ /**
28
+ * @param {TreeNode } root
29
+ * @return {number }
30
+ */
31
+ let sumOfLeftLeaves = function ( root ) {
32
+ // Deal with the first root value
33
+ if ( ! root || root . val === null || ( ! root . left && ! root . right ) ) {
34
+ return 0 ;
35
+ }
36
+
37
+ let leftlefVal = 0 , rightlefVal = 0 ;
38
+ if ( root . left ) {
39
+ if ( ! root . left . left && ! root . left . right ) {
40
+ leftlefVal = root . left . val ;
41
+ } else {
42
+ leftlefVal = sumOfLeftLeaves ( root . left ) ;
43
+ }
44
+ }
45
+
46
+ if ( root . right ) {
47
+ if ( ! root . right . left && ! root . right . right ) {
48
+ rightlefVal = 0 ;
49
+ } else {
50
+ rightlefVal = sumOfLeftLeaves ( root . right ) ;
51
+ }
52
+ }
53
+
54
+ return leftlefVal + rightlefVal ;
55
+ } ;
You can’t perform that action at this time.
0 commit comments