Skip to content

Commit 3e2d885

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

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

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

5-
Progress: 9/
5+
Progress: 12/
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|
11+
|66|[Plus One](https://leetcode.com/problems/plus-one/) | [JavaScript](./src/plus-one/res.js)|Easy|
1112
|175|[Combine Two Tables](https://leetcode.com/problems/combine-two-tables/)| [SQL](./src/combine-two-tables/res.txt)|Easy|
1213
|176|[Second Highest Salary](https://leetcode.com/problems/second-highest-salary/)| [SQL](./src/second-highest-salary/res.txt)|Easy|
1314
|177|[Nth Highest Salary](https://leetcode.com/problems/nth-highest-salary/)| [SQL](./src/nth-highest-salary/res.txt)|Medium|
1415
|181|[Employees Earning More Than Their Managers](https://leetcode.com/problems/employees-earning-more-than-their-managers/) | [SQL](./src/employees-earning-more-than-their-managers/res.txt)|Easy|
1516
|182|[Duplicate Emails](https://leetcode.com/problems/duplicate-emails/) | [SQL](./src/duplicate-emails/res.txt)|Easy|
1617
|184|[Department Highest Salary](https://leetcode.com/problems/department-highest-salary/) | [SQL](./src/department-highest-salary/res.txt)|Medium|
1718
|197|[Rising Temperature](https://leetcode.com/problems/rising-temperature/) | [SQL](./src/rising-temperature/res.txt)|Easy|
19+
|342|[Power of Four](https://leetcode.com/problems/power-of-four/) | [JavaScript](./src/power-of-four/res.js)|Easy|
1820
|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/plus-one/res.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
3+
*
4+
* You may assume the integer do not contain any leading zero, except the number 0 itself.
5+
*
6+
* The digits are stored such that the most significant digit is at the head of the list.
7+
*
8+
* res.js
9+
* @authors Joe Jiang ([email protected])
10+
* @date 2017-02-18 19:41:09
11+
* @version $Id$
12+
*/
13+
14+
/**
15+
* @param {number[]} digits
16+
* @return {number[]}
17+
*/
18+
let plusOne = function(digits) {
19+
let carrybit = 1,
20+
res = [];
21+
22+
for (let i = digits.length - 1; i >= 0; i--) {
23+
let currentval = digits[i] + carrybit;
24+
if (currentval>9) {
25+
carrybit=1;
26+
currentval%=10;
27+
} else {
28+
carrybit=0;
29+
}
30+
res.unshift(currentval)
31+
}
32+
33+
if (carrybit===1) {
34+
res.unshift(1);
35+
}
36+
37+
return res;
38+
};

src/power-of-four/res.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
3+
*
4+
* Example:
5+
*
6+
* Given num = 16, return true. Given num = 5, return false.
7+
*
8+
* Follow up: Could you solve it without loops/recursion?
9+
*
10+
* Hint: We know that if number is power of two, it satisfies that n & (n – 1) equals zero, that is: n & (n – 1) removes the least significant bit, leaving the number (that is power of two) zero. We also know that mathematically, 4^n - 1 can be divided by 3.
11+
*
12+
* @authors Joe Jiang ([email protected])
13+
* @date 2017-02-18 20:13:10
14+
* @version $Id$
15+
*/
16+
17+
/**
18+
* @param {number} num
19+
* @return {boolean}
20+
*/
21+
let isPowerOfFour = function(num) {
22+
let compNum = num-1;
23+
if (num<1 || num & compNum) {
24+
return false;
25+
}
26+
if (compNum%3 !== 0) {
27+
return false;
28+
}
29+
30+
return true;
31+
};

0 commit comments

Comments
 (0)