Skip to content

Commit 0736064

Browse files
committed
add some modification and new problem
1 parent 75ce46f commit 0736064

7 files changed

+209
-24
lines changed

2 Add Two Numbers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
2+
3+
// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
4+
// Output: 7 -> 0 -> 8
5+
6+
17
/**
28
* Definition for singly-linked list.
39
* function ListNode(val) {
@@ -10,6 +16,11 @@
1016
* @param {ListNode} l2
1117
* @return {ListNode}
1218
*/
19+
20+
21+
22+
// value reverse helps to asslign the first digit or both linklist
23+
1324
var addTwoNumbers = function(l1, l2) {
1425
if(l1 === null || l2 === null){
1526
return l1 || l2;

3 Longest Substring Without Repeating Characters.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* @param {string} s
33
* @return {number}
44
*/
5+
6+
57
var lengthOfLongestSubstring = function(s) {
68
if(s === null || s.length === 0){
79
return 0;
@@ -11,16 +13,17 @@ var lengthOfLongestSubstring = function(s) {
1113
var len = 0;
1214
var maxLen = len;
1315
var start = 0;
14-
16+
17+
// scan from left to right.
1518
for(var i = start; i < s.length; i++){
1619
c = s[i];
1720

1821
if(map[c] !== undefined && map[c] >= start) {
19-
start = map[c] + 1;
20-
len = i - start;
22+
start = map[c] + 1; // start new search with repeated word's last location + 1
23+
len = i - start; // real length -> from for example 3 to 5 is 3, so it's 5-3+1 and + 1 happens later
2124
}
2225

23-
len++;
26+
len++; // real length -> from for example 3 to 5 is 3, so it's 5-3+1 and + 1 happens later
2427

2528
if(len > maxLen){
2629
maxLen = len;

5 Longest Palindromic Substring.js

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1+
// Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
2+
13
/**
24
* @param {string} s
35
* @return {string}
46
*/
57

8+
9+
610
var longestPalindrome = function(s) {
711
if(s === null || s.length === 0){
812
return "";
913
}
1014

1115
var result = "";
16+
17+
// The reason to multiply by 2 is because
18+
// Palindrome can be in two forms
19+
// 1. abba There will be case which center has two idenitical charachter,
20+
// And there will be maximum 2*n - 1 such case
21+
// 2. aba There will be case which center has only one character
22+
var len = 2*s.length - 1;
23+
var left, right;
1224

13-
for(var i = (2*s.length) - 1; i--;){
14-
var left = parseInt(i/2);
15-
var right = parseInt(i/2);
25+
for(var i = 0; i < len; i++){
26+
left = right = parseInt(i/2);
1627
if(i%2 === 1){
1728
right++;
1829
}
1930

20-
var str = lengthOfPalindrome(s,left,right);
31+
var str = expandFromCenterAndCheckForPalindrome(s,left,right);
2132

2233
if(str.length > result.length){
2334
result = str;
@@ -27,7 +38,49 @@ var longestPalindrome = function(s) {
2738
};
2839

2940

30-
var lengthOfPalindrome = function(s, left, right){
41+
// other implementation
42+
43+
var longestPalindrome = function(s) {
44+
if(s === null || s.length === 0){
45+
return "";
46+
}
47+
48+
var result = "";
49+
50+
// The reason to multiply by 2 is because
51+
// Palindrome can be in two forms
52+
// 1. abba There will be case which center has two idenitical charachter,
53+
// And there will be maximum 2*n - 1 such case
54+
// 2. aba There will be case which center has only one character
55+
var len = s.length;
56+
var left, right;
57+
58+
for(var i = 0; i < len; i++){
59+
left = right = i;
60+
61+
var str = expandFromCenterAndCheckForPalindrome(s,left,right);
62+
if(str.length > result.length){
63+
result = str;
64+
}
65+
var str = expandFromCenterAndCheckForPalindrome(s,left,right + 1);
66+
if(str.length > result.length){
67+
result = str;
68+
}
69+
}
70+
return result;
71+
};
72+
73+
74+
75+
var expandFromCenterAndCheckForPalindrome = function(s, left, right){
76+
// in the case where left !== right
77+
// that's the case "abba"
78+
// which it checks for if b === b then a === a
79+
80+
// in the case where left === right
81+
// that's the case "aba"
82+
// which it check if b === b as left === right
83+
// then a === a
3184
while(left >= 0 && right < s.length && s[left] === s[right]){
3285
left--;
3386
right++;

9 Palindrome Number.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,44 @@
22
// Language: Javascript
33
// Problem: https://leetcode.com/problems/palindrome-number/
44
// Author: Chihung Yu
5-
/**
6-
* Leetcode Question
5+
6+
// Determine whether an integer is a palindrome. Do this without extra space.
7+
8+
/**
79
* @param {number} x
810
* @return {boolean}
911
*/
10-
1112
var isPalindrome = function(x) {
12-
if(x < 0){
13+
if(x < 0) {
1314
return false;
1415
}
1516

1617
var div = 1;
1718

18-
while(parseInt(x/div) >= 10){
19-
div *= 10;
19+
// it means that the div is still a valid divider
20+
// e.g 600 the divider should be 100 at maximum
21+
// e.g. 90 the divider should be 10 at maximum
22+
// e.g. 1 the divider should be a 1 at maximum
23+
while(parseInt(x/div) >= 10) {
24+
div *= 10;
2025
}
2126

22-
while(x !== 0){
23-
var l = parseInt(x/div);
24-
var r = x%10;
25-
26-
if(l !== r){
27+
var left, right;
28+
29+
// when div === 1 it means the digit only has one value to examine
30+
// e.g. 121 -> only 2 is left for examine which can be ignore
31+
// e.g. 1 -> 1
32+
// e.g. 43234 -> 2
33+
while(div > 1) {
34+
left = parseInt(x/div);
35+
right = x%10;
36+
if(left !== right) {
2737
return false;
2838
}
2939

30-
x = x % div;
31-
x = parseInt(x/10);
32-
33-
div = parseInt(div/100);
40+
x = x%div; // remove the left most digit
41+
x = parseInt(x/10); // remove the right most digit
42+
div /= 100;
3443
}
3544

3645
return true;

Jump Game II.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var jump = function(nums) {
6+
var curMax = 0;
7+
var nJumps = 0;
8+
var i = 0;
9+
var n = nums.length;
10+
11+
while(curMax < n - 1) {
12+
var lastMax = curMax;
13+
// go through covered area
14+
for(; i <= lastMax; i++) {
15+
curMax = Math.max(curMax, i+nums[i]);
16+
}
17+
nJumps++;
18+
// if cannot make progress in the covered area, give up
19+
if(lastMax === curMax) {
20+
return -1;
21+
}
22+
}
23+
24+
return nJumps;
25+
};

Reverse Integer.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number} x
3+
* @return {number}
4+
*/
5+
var reverse = function(x) {
6+
var isNeg = x < 0;
7+
var result = 0;
8+
9+
x = Math.abs(x);
10+
11+
while(x) {
12+
var lastDigit = x%10;
13+
result *= 10;
14+
result += lastDigit;
15+
x = parseInt(x/10);
16+
}
17+
18+
result = isNeg ? -result : result;
19+
20+
if(result > Math.pow(2,31) - 1 || result < -Math.pow(2,31)) {
21+
return 0;
22+
}
23+
24+
return result;
25+
};

find kth element in two arrays.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var findKth = (a1, a2, kth) => {
2+
var alen1 = a1.length;
3+
var alen2 = a2.length;
4+
5+
if(alen1 > alen2) {
6+
return findKth(a2, a1, kth);
7+
}
8+
9+
if(alen1 === 0) {
10+
return a2[kth -1];
11+
}
12+
13+
if(kth === 1) {
14+
return Math.min(a1[0], a2[0]);
15+
}
16+
17+
// need to make sure that kth is not 1 as kth/2 = 0
18+
var p1 = Math.min(parseInt(kth/2), alen1);
19+
var p2 = kth - p1;
20+
21+
if(a1[p1 - 1] < a2[p2 - 1]) {
22+
return findKth(a1.slice(p1), a2, kth - p1);
23+
} else if(a1[p1 - 1] > a2[p2 - 1]) {
24+
return findKth(a1, a2.slice(p2), kth - p2);
25+
} else {
26+
return a1[p1 - 1];
27+
}
28+
}
29+
30+
31+
// function findKth(a, aStart, aEnd, b, bStart, bEnd, kth) {
32+
// // use array position rather than use slice to get array
33+
// // always assume that m is equal or smaller than n
34+
// var aLen = aEnd - aStart + 1; // same as a.length as index 3 - 3 = 0 but still it has one element compared to a.length === 0 has notthing
35+
// var bLen = bEnd - bStart + 1;
36+
// var mid;
37+
// if(aLen > bLen) {
38+
// return findKth(b, bStart, bEnd, a, aStart, aEnd, kth);
39+
// }
40+
41+
// if(aLen <= 0) {
42+
// return b[kth - 1];
43+
// }
44+
45+
// if(kth === 1) {
46+
// return Math.min(a[aStart], b[bStart]);
47+
// }
48+
49+
// var k1 = Math.min(parseInt(kth/2), aLen);
50+
// var k2 = kth - k1;
51+
52+
// if(a[aStart + k1 - 1] < b[bStart + k2 - 1]) {
53+
// return findKth(a, aStart + k1, aEnd, b, bStart, bEnd, kth -k1);
54+
// } else if(a[aStart + k1 - 1] > b[bStart + k2 - 1]) {
55+
// return findKth(a, aStart, aEnd, b, bStart + k2, bEnd, kth -k2);
56+
// } else {
57+
// return a[aStart + k1 -1];
58+
// }
59+
// }

0 commit comments

Comments
 (0)