-
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
60 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,10 @@ | ||
/** | ||
Do not return anything, modify nums1 in-place instead. | ||
*/ | ||
function merge(nums1: number[], m: number, nums2: number[], n: number): void { | ||
nums1.splice(m, nums1.length); | ||
nums1.push(...nums2); | ||
nums1.sort((a, b) => a - b); | ||
} | ||
|
||
// TODO: solve it without using built-in function |
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,12 @@ | ||
// https://leetcode.com/problems/number-of-1-bits/solutions/3385115/javascript-typescript-solution-explanation/ | ||
|
||
function hammingWeight(n: number): number { | ||
let count = 0; | ||
let binaryString = n.toString(2); // convert number to its binary (base 2) | ||
|
||
for (const char of binaryString) { | ||
if (char === '1') count++; | ||
} | ||
|
||
return count; | ||
} |
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,38 @@ | ||
// Solution 1: using brute force O(n^2) | ||
function twoSum(nums: number[], target: number): number[] { | ||
for (let i = 0; i < nums.length; i++) { | ||
for (let j = i + 1; j < nums.length; j++) { | ||
if (nums[i] + nums[j] === target) return [i, j]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
// Solution 2: sort + two pointers O(nlogn) | ||
function twoSum2(nums: number[], target: number): number[] { | ||
const sortedNums = [...nums].sort((a, b) => a - b); | ||
|
||
let l = 0; | ||
let r = sortedNums.length - 1; | ||
|
||
while (l < r) { | ||
const sum = sortedNums[l] + sortedNums[r]; | ||
|
||
if (sum === target) { | ||
const leftIndex = nums.indexOf(sortedNums[l]); | ||
const rightIndex = nums.lastIndexOf(sortedNums[r]); | ||
return [leftIndex, rightIndex]; | ||
} | ||
|
||
if (sum < target) { | ||
l++; | ||
} else { | ||
r--; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
// TODO: add another approach |