-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
6760527
commit 9471e68
Showing
9 changed files
with
258 additions
and
185 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
src/page-1/35. Search Insert Position/search-insert.test.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
src/page-1/35. Search Insert Position/searchInsert.test.ts
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,15 @@ | ||
import { searchInsert, searchInsert2 } from './searchInsert'; | ||
|
||
describe('35. Search Insert Position', () => { | ||
test('searchInsert', () => { | ||
expect(searchInsert([1, 3, 5, 6], 5)).toBe(2); | ||
expect(searchInsert([1, 3, 5, 6], 2)).toBe(1); | ||
expect(searchInsert([1, 3, 5, 6], 7)).toBe(4); | ||
}); | ||
|
||
test('searchInsert2', () => { | ||
expect(searchInsert2([1, 3, 5, 6], 5)).toBe(2); | ||
expect(searchInsert2([1, 3, 5, 6], 2)).toBe(1); | ||
expect(searchInsert2([1, 3, 5, 6], 7)).toBe(4); | ||
}); | ||
}); |
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,36 @@ | ||
type SearchInsert = (nums: number[], target: number) => number; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const searchInsert: SearchInsert = (nums, target) => { | ||
let index = 0; | ||
|
||
while (index < nums.length && nums[index] <= target) { | ||
if (nums[index] === target) return index; | ||
index += 1; | ||
} | ||
|
||
return index; | ||
}; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const searchInsert2: SearchInsert = (nums, target) => { | ||
let left = 0; | ||
let right = nums.length - 1; | ||
|
||
while (left <= right) { | ||
const mid = Math.floor((left + right) / 2); | ||
if (nums[mid] === target) return mid; | ||
|
||
if (nums[mid] < target) { | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
|
||
return left; | ||
}; |
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 @@ | ||
import { myAtoi } from './myAtoi'; | ||
|
||
describe('8. String to Integer (atoi)', () => { | ||
test('myAtoi', () => { | ||
expect(myAtoi('42')).toBe(42); | ||
expect(myAtoi(' -042')).toBe(-42); | ||
expect(myAtoi('1337c0d3')).toBe(1337); | ||
expect(myAtoi('0-1')).toBe(0); | ||
expect(myAtoi('words and 987')).toBe(0); | ||
}); | ||
}); |
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,37 @@ | ||
export function myAtoi(s: string): number { | ||
const INT_MAX = 2 ** 31 - 1; | ||
const INT_MIN = -(2 ** 31); | ||
|
||
let i = 0; | ||
let sign = 1; | ||
let result = 0; | ||
|
||
// Ignore leading whitespaces | ||
while (i < s.length && s[i] === ' ') { | ||
i += 1; | ||
} | ||
|
||
// Check for sign | ||
if (i < s.length && (s[i] === '-' || s[i] === '+')) { | ||
sign = s[i] === '-' ? -1 : 1; | ||
i += 1; | ||
} | ||
|
||
// Convert digits to integer | ||
while (i < s.length && s[i] >= '0' && s[i] <= '9') { | ||
const digit = s.charCodeAt(i) - '0'.charCodeAt(0); | ||
|
||
// Handle overflow and underflow | ||
if ( | ||
result > Math.floor(INT_MAX / 10) || | ||
(result === Math.floor(INT_MAX / 10) && digit > INT_MAX % 10) | ||
) { | ||
return sign === 1 ? INT_MAX : INT_MIN; | ||
} | ||
|
||
result = result * 10 + digit; | ||
i += 1; | ||
} | ||
|
||
return result * sign; | ||
} |