Skip to content

Commit 1db72e4

Browse files
committed
update: 2 solutions
1 parent 3e2d885 commit 1db72e4

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
This is the solution collection of my LeetCode problems, most of them are programmed in JavaScript.
44

5-
Progress: 12/
5+
Progress: 14/
66

77
| ID | Title | Solution | Difficulty |
88
|---| ----- | -------- | ---------- |
99
|1|[Two Sum](https://leetcode.com/problems/two-sum/) | [JavaScript](./src/two-sum/res.js)|Easy|
1010
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [JavaScript](./src/add-two-numbers/res.js)|Medium|
1111
|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|
1213
|175|[Combine Two Tables](https://leetcode.com/problems/combine-two-tables/)| [SQL](./src/combine-two-tables/res.txt)|Easy|
1314
|176|[Second Highest Salary](https://leetcode.com/problems/second-highest-salary/)| [SQL](./src/second-highest-salary/res.txt)|Easy|
1415
|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/
1718
|184|[Department Highest Salary](https://leetcode.com/problems/department-highest-salary/) | [SQL](./src/department-highest-salary/res.txt)|Medium|
1819
|197|[Rising Temperature](https://leetcode.com/problems/rising-temperature/) | [SQL](./src/rising-temperature/res.txt)|Easy|
1920
|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|
2022
|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|

src/sqrtx/res.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
};

src/sum-of-left-leaves/res.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
};

0 commit comments

Comments
 (0)