-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |