Skip to content

Commit

Permalink
feat(LeetCode): add solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
plskz committed Mar 27, 2023
1 parent 010f828 commit 3bef6de
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions LeetCode/reverse-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
Do not return anything, modify s in-place instead.
*/
function reverseString(s: string[]): void {
let l = 0;
let r = s.length - 1;

while (l < r) {
[s[l], s[r]] = [s[r], s[l]];
l++, r--;
}
}

reverseString(['h', 'e', 'l', 'l', 'o']); // [ 'o', 'l', 'l', 'e', 'h' ]
19 changes: 19 additions & 0 deletions LeetCode/reverse-vowels-of-a-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function reverseVowels(s: string): string {
const vowels = 'aeiouAEIOU';
let res = s.split('');

let l = 0;
let r = s.length - 1;

while (l < r) {
if (!vowels.includes(res[l])) l++;
if (!vowels.includes(res[r])) r--;

if (!vowels.includes(res[l]) || !vowels.includes(res[r])) continue;

[res[l], res[r]] = [res[r], res[l]];
l++, r--;
}

return res.join('');
}
11 changes: 11 additions & 0 deletions LeetCode/third-maximum-number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function thirdMax(nums: number[]): number {
const s = [...new Set<number>(nums)].sort((a, b) => b - a);

// return s.length >= 3 ? s[2] : s[0];
return s[2] ?? s[0]; // same above
}

console.log(thirdMax([3, 2, 1])); // 1
console.log(thirdMax([1, 2])); // 2
console.log(thirdMax([2, 2, 3, 1])); // 1
console.log(thirdMax([1, 1, 2])); // 2

0 comments on commit 3bef6de

Please sign in to comment.