Skip to content

Commit 12a70f0

Browse files
author
beck chen
committed
checkin leetcode practice
1 parent 88a2e48 commit 12a70f0

File tree

7 files changed

+223
-0
lines changed

7 files changed

+223
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
var longestPalindrome = function (s) {
6+
const charsArr = s.split('');
7+
let result = [];
8+
let longestLength = 0;
9+
10+
// handle odd cases
11+
for (let ii = 0; ii < charsArr.length; ii++) {
12+
let offset = 0;
13+
while (isValidIndex(charsArr, ii - offset - 1)
14+
&& isValidIndex(charsArr, ii + offset + 1)
15+
&& charsArr[ii - offset - 1] === charsArr[ii + offset + 1]) {
16+
offset++;
17+
}
18+
19+
const currentLength = offset * 2 + 1;
20+
if (currentLength > longestLength) {
21+
longestLength = currentLength;
22+
result = [ii - offset, ii + offset];
23+
}
24+
}
25+
26+
console.log(result);
27+
28+
// handle even cases
29+
for (let ii = 0; ii < charsArr.length; ii++) {
30+
let offset = 0;
31+
while (isValidIndex(charsArr, ii - offset)
32+
&& isValidIndex(charsArr, ii + offset + 1)
33+
&& charsArr[ii - offset] === charsArr[ii + offset + 1]) {
34+
offset++;
35+
}
36+
37+
const currentLength = offset * 2;
38+
if (currentLength > longestLength) {
39+
longestLength = currentLength;
40+
result = [ii - offset + 1, ii + offset];
41+
}
42+
}
43+
44+
return s.substring(result[0], result[1] + 1);
45+
};
46+
47+
function isValidIndex(charsArr, index) {
48+
return index >= 0 && index < charsArr.length;
49+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {character[]} s
3+
* @return {void} Do not return anything, modify s in-place instead.
4+
*/
5+
var reverseString = function (s) {
6+
const originalLength = s.length;
7+
for (let ii = s.length - 2; ii >= 0; ii--) {
8+
s.push(s[ii]);
9+
}
10+
11+
s.splice(0, originalLength - 1);
12+
};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
** Given an input string s, reverse the order of the words.
3+
** A word is defined as a sequence of non-space characters.
4+
** The words in s will be separated by at least one space.
5+
** Return a string of the words in reverse order concatenated by a single space.
6+
** Note that s may contain leading or trailing spaces or multiple spaces between two words.
7+
** The returned string should only have a single space separating the words. Do not include any extra spaces.
8+
*/
9+
10+
// Input: s = "the sky is blue"
11+
// Output: "blue is sky the"
12+
13+
/**
14+
* @param {string} s
15+
* @return {string}
16+
*/
17+
var reverseWords = function (s) {
18+
const stringAsArr = trimSpacesAndReverse(s);
19+
20+
// loop through characters array
21+
let startIndex = 0, endIndex = 0;
22+
while (endIndex <= stringAsArr.length) {
23+
if (stringAsArr[endIndex + 1] === ' ') {
24+
reverseChars(stringAsArr, startIndex, endIndex);
25+
endIndex += 2;
26+
startIndex = endIndex;
27+
} else {
28+
endIndex++;
29+
}
30+
}
31+
32+
reverseChars(stringAsArr, startIndex, stringAsArr.length - 1);
33+
return stringAsArr.join('');
34+
};
35+
36+
function trimSpacesAndReverse(str) {
37+
let startIndex = 0, endIndex = str.length - 1;
38+
while (str[startIndex] === ' ') {
39+
startIndex++;
40+
}
41+
42+
while (str[endIndex] === ' ') {
43+
endIndex--;
44+
}
45+
46+
// remove extra spaces in between
47+
const trimmedArr = [];
48+
for (let ii = endIndex; ii >= startIndex; ii--) {
49+
if (str[ii] !== ' ') {
50+
trimmedArr.push(str[ii]);
51+
}
52+
53+
if (str[ii] === ' ' && str[ii - 1] !== ' ') {
54+
trimmedArr.push(str[ii]);
55+
}
56+
}
57+
58+
return trimmedArr;
59+
}
60+
61+
function reverseChars(charArr, startIndex, endIndex) {
62+
while (startIndex < endIndex) {
63+
[charArr[startIndex], charArr[endIndex]] = [charArr[endIndex], charArr[startIndex]];
64+
startIndex++;
65+
endIndex--;
66+
}
67+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
** Given a character array s, reverse the order of the words.
3+
** A word is defined as a sequence of non-space characters.
4+
** The words in s will be separated by a single space.
5+
** Your code must solve the problem in-place, i.e. without allocating extra space.
6+
*/
7+
8+
9+
/**
10+
* @param {character[]} str
11+
* @return {void} Do not return anything, modify str in-place instead.
12+
*/
13+
var reverseWords = function (str) {
14+
reverseChars(str, 0, str.length - 1);
15+
16+
// loop from beginning of arr to second to last character,
17+
// look for start of string, and look for space, and
18+
// reverse the characters in between
19+
let wordStartIndex = 0;
20+
for (let ii = 0; ii < str.length - 1; ii++) {
21+
if (str[ii + 1] === ' ') {
22+
reverseChars(str, wordStartIndex, ii);
23+
wordStartIndex = ii + 2;
24+
}
25+
}
26+
27+
// reverse the last word
28+
reverseChars(str, wordStartIndex, str.length - 1);
29+
return str;
30+
};
31+
32+
function reverseChars(wordsToReverseArr, firstIndex, lastIndex) {
33+
while (firstIndex < lastIndex) {
34+
[wordsToReverseArr[firstIndex], wordsToReverseArr[lastIndex]]
35+
= [wordsToReverseArr[lastIndex], wordsToReverseArr[firstIndex]];
36+
firstIndex++;
37+
lastIndex--;
38+
}
39+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var twoSum = function (nums, target) {
2+
// use hash table
3+
if (target === undefined || !nums || nums.length === 0) { return []; }
4+
const map = {};
5+
6+
for (let ii = 0; ii < nums.length; ii++) {
7+
if (map[target - nums[ii]] !== undefined) {
8+
return [map[target - nums[ii]], ii];
9+
}
10+
11+
map[nums[ii]] = ii;
12+
}
13+
14+
return [];
15+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function (s) {
6+
s = s.toLowerCase().replace(/[^a-z0-9]/g, "");
7+
// use two pointer solution
8+
let startIndex = 0, endIndex = s.length - 1;
9+
while (startIndex <= endIndex) {
10+
if (s[startIndex] !== s[endIndex]) { return false; }
11+
startIndex++;
12+
endIndex--;
13+
}
14+
15+
return true
16+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function (s) {
6+
if (!s || s.length === 0) { return false; }
7+
const map = {
8+
']': '[',
9+
'}': '{',
10+
')': '('
11+
};
12+
13+
const stack = [];
14+
for (let ii = 0; ii < s.length; ii++) {
15+
if (!map[s[ii]]) {
16+
stack.push(s[ii]);
17+
} else {
18+
if (map[s[ii]] !== stack.pop()) {
19+
return false;
20+
}
21+
}
22+
}
23+
24+
return stack.length === 0;
25+
};

0 commit comments

Comments
 (0)