Skip to content

Commit 0816077

Browse files
committed
back to leetcode
1 parent c99a218 commit 0816077

File tree

8 files changed

+173
-4
lines changed

8 files changed

+173
-4
lines changed

Easy/125-validPalindrome.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Key: use regular expression to find only alpha and numeric letters.
33
* Two pointers: start and end
4-
*
4+
*
55
* @param {string} s
66
* @return {boolean}
77
*/
@@ -10,7 +10,7 @@ var isPalindrome = function(s) {
1010
s = s.match(pattern);
1111
if (!s || s.length === 1) return true;
1212
for (var i = 0; i < s.length / 2; i++) {
13-
if (s[i].toLowerCase() !== s[s.length - 1 -i].toLowerCase()) return false;
13+
if (s[i].toLowerCase() !== s[s.length - 1 - i].toLowerCase()) return false;
1414
}
1515

1616
return true;

Easy/412-fizzBuzz.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
*/
5+
var fizzBuzz = function(n) {
6+
var res = [];
7+
8+
for (var i = 1; i <= n; i++) {
9+
if (i % 15 === 0) {
10+
res.push('FizzBuzz');
11+
} else if (i % 5 === 0) {
12+
res.push('Buzz');
13+
} else if (i % 3 === 0) {
14+
res.push('Fizz');
15+
} else {
16+
res.push('' + i);
17+
}
18+
}
19+
20+
return res;
21+
};
22+
23+
// try without %
24+
var fizzBuzz = function(n) {
25+
var res = [];
26+
27+
for (var i = 1, fizz = 0, buzz = 0; i <= n; i++) {
28+
if (fizz === 3 && buzz === 5) {
29+
res.push('FizzBuzz');
30+
fizz = 0;
31+
buzz = 0;
32+
} else if (buzz === 5) {
33+
res.push('Buzz');
34+
buzz = 0;
35+
} else if (fizz === 3) {
36+
res.push('Fizz');
37+
fizz = 0;
38+
} else {
39+
res.push('' + i);
40+
fizz++;
41+
buzz++;
42+
}
43+
}
44+
45+
return res;
46+
};

Easy/70-climbChairs.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ var climbStairs = function(n) {
1919
return result;
2020
};
2121

22+
// 2nd try
23+
var climbStairs = function(n) {
24+
if (n <= 2) return n;
25+
var stepOne = 1;
26+
var stepTwo = 2;
27+
var result;
28+
for (var i = 2; i < n; i++) {
29+
result = stepOne + stepTwo;
30+
stepOne = stepTwo;
31+
stepTwo = result;
32+
}
33+
34+
return result;
35+
};
36+
2237
// recursive, exponential complexity. Not accepted
2338
var climbStairs = function(n) {
2439
if (n === 1) return 1;

Hard/287-findDuplicateNumber.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Solution: Binary search [1, n-1]; find the mid number, count how many numbers
3+
* are less than or equal to mid, if the count is more than mid, then the duplicates
4+
* must exist in [1, mid], otherwise [mid+1, hi].
5+
* e.g. [1,10], mid = 5,if the count of number that is <= 5 in nums (the array), then
6+
* the duplicate number must be in [1,5] .... continue this binary search in this half.
7+
*
8+
* @param {number[]} nums
9+
* @return {number}
10+
*/
11+
var findDuplicate = function(nums) {
12+
var n = nums.length;
13+
var lo = 1;
14+
var hi = n - 1;
15+
16+
while (lo < hi) {
17+
var mid = lo + Math.floor((hi - lo) / 2);
18+
var count = 0;
19+
for (var i = 0; i < n; i++) {
20+
if (nums[i] <= mid) {
21+
count++;
22+
}
23+
}
24+
if (count <= mid) {
25+
lo = mid + 1;
26+
} else {
27+
hi = mid;
28+
}
29+
}
30+
31+
return lo;
32+
};

Medium/122-bestTimeToBuySellStockII.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* @param {number[]} prices
66
* @return {number}
77
*/
8+
// This solution in fact doesn't follow the rule (you're not allowed to buy and sell at the same time),
9+
// But mathmatically it is right.
810
var maxProfit = function(prices) {
911
var maxProfit = 0;
1012
for (var i = 1; i < prices.length; i++) {
@@ -14,3 +16,22 @@ var maxProfit = function(prices) {
1416

1517
return maxProfit;
1618
};
19+
20+
// 2nd try, my own solution
21+
var maxProfit = function(prices) {
22+
var maxProfit = 0;
23+
var minPrice = prices[0];
24+
for (var i = 0; i < prices.length;) {
25+
i++;
26+
while (prices[i] > prices[i-1]) {
27+
i++;
28+
}
29+
maxProfit += prices[i-1] - minPrice;
30+
minPrice = prices[i];
31+
}
32+
return maxProfit;
33+
};
34+
35+
// test cases
36+
// [5,4,1,2,7,8]
37+
// [1,2,5,4,6,8]

Medium/268-missingNumber.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ var missingNumber = function(nums) {
1111
return nSum - numsSum;
1212
};
1313

14+
var missingNumber = function(nums) {
15+
var length = nums.length;
16+
var total = 0.5 * length * (length + 1);
17+
var numSum = 0;
18+
for (var i = 0; i < length; i++) {
19+
numSum += nums[i];
20+
}
21+
22+
return total - numSum;
23+
};
24+
1425
// solution 2 use bit manipulation
1526
var missingNumber = function(nums) {
1627
var missNum = 0;

Medium/284-peekingIterator.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// No JavaScript supported
2+
// peek() means peek next elment but pointer doesn't move (i.e. still points the next elment)
3+
// Solution, think the iterator as the the pointer, cache next value
4+
// Java Iterator interface reference:
5+
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
6+
class PeekingIterator implements Iterator<Integer> {
7+
Integer next = null;
8+
Iterator<Integer> iter;
9+
10+
public PeekingIterator(Iterator<Integer> iterator) {
11+
// initialize any member here.
12+
this.iter = iterator;
13+
if (iter.hasNext()) {
14+
this.next = iter.next();
15+
}
16+
}
17+
18+
// Returns the next element in the iteration without advancing the iterator.
19+
public Integer peek() {
20+
return this.next;
21+
}
22+
23+
// hasNext() and next() should behave the same as in the Iterator interface.
24+
// Override them if needed.
25+
@Override
26+
public Integer next() {
27+
Integer nextCache = next;
28+
if (iter.hasNext()) {
29+
next = iter.next();
30+
} else {
31+
next = null;
32+
}
33+
34+
return nextCache;
35+
}
36+
37+
@Override
38+
public boolean hasNext() {
39+
return next != null;
40+
}
41+
}

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
* [326. Power of Three](https://leetcode.com/problems/power-of-three/) - [Solution](./Easy/326-powerOfThree.js)
6969
* [328. Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) - [Solution](./Easy/328-oddevenLinkedList.js)
7070
* [342. Power of Four](https://leetcode.com/problems/power-of-four/) - [Solution](./Easy/342-powerOfFour.js)
71+
* [412. Fizz Buzz](https://leetcode.com/problems/fizz-buzz/?tab=Solutions) - [Solution](./Easy/412-fizzBuzz.js)
7172

7273

7374
##### Medium
@@ -106,8 +107,8 @@
106107
* [71. Simplify Path](https://leetcode.com/problems/simplify-path/) - [Solution](./Medium/71-simplifyPath.js)
107108
* [73. Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) - [Solution](./Medium/73-setMatrixZeroes.js)
108109
* [74. Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) - [Solution](./Medium/74-search2DMatrix.js)
109-
* [75. Sort Colors](https://leetcode.com/problems/sort-colors/) - [Solution](./Medium/75-sortColors.js)
110-
* [77. Combinations](https://leetcode.com/problems/combinations/) - [Solution](./Medium/77-combinations.js)
110+
* [75. Sort Colors](https://leetcode.com/problems/sort-colors/) - [Solution](./Medium/75-sortColors.js)
111+
* [77. Combinations](https://leetcode.com/problems/combinations/) - [Solution](./Medium/77-combinations.js)
111112
* [78. Subsets](https://leetcode.com/problems/subsets/) - [Solution](./Medium/78-subsets.js)
112113
* [79. Word Search](https://leetcode.com/problems/word-search/) - [Solution](./Medium/79-wordSearch.js)
113114
* [80. Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) - [Solution](./Medium/80-removeDuplicatesII.js)
@@ -163,6 +164,7 @@
163164
* [260. Single Number III](https://leetcode.com/problems/single-number-iii/) - [Solution](./Medium/260-singleNumberIII.js)
164165
* [268. Missing Number](https://leetcode.com/problems/missing-number/) - [Solution](./Medium/268-missingNumber.js)
165166
* [274. H-Index](https://leetcode.com/problems/h-index/) - [Solution](./Medium/274-hIndex.js)
167+
* [284. Peeking Iterator](https://leetcode.com/problems/peeking-iterator/) - [Solution](./Medium/284-peekingIterator.java)
166168
* [300. Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) - [Solution](./Medium/300-longestIncreasingSubsequence.js)
167169
* [309. Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) - [Solution](./Medium/309-bestTimeStockCooldown.js)
168170
* [318. Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) - [Solution](./Medium/318-maximumProductWordLengths.js)
@@ -186,3 +188,4 @@
186188
* [132. Palindrome Partitioning II](https://leetcode.com/problems/palindrome-partitioning-ii/) - [Solution](./Hard/132-palindromePartitioningII.js)
187189
* [145. Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) - [Solution](./Hard/145-binaryTreePostorder.js)
188190
* [154. Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/) - [Solution](./Hard/154-findMinimumInRotatedSortedArray-II.js)
191+
* [287. Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) - [Solution](./Hard/287-findDuplicateNumber.js)

0 commit comments

Comments
 (0)