Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

0704: Binary Search#2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
golevy merged 1 commit intodevelopfromlevy/binary-search
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions0704-Binary_Search/main.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
const nums = [-1, 0, 3, 5, 9, 12];
const target1 = 9;
const target2 = 2;
function searchUsingLCRC(nums, target) {
let mid, left = 0, right = nums.length - 1;
while (left <= right) {
mid = left + ((right - left) >> 1);
if (nums[mid] > target) {
right = mid - 1;
}
else if (nums[mid] < target) {
left = mid + 1;
}
else {
return mid;
}
}
return -1;
}
function searchUsingLCRO(nums, target) {
let mid, left = 0, right = nums.length;
while (left < right) {
mid = left + ((right - left) >> 1);
if (nums[mid] > target) {
right = mid;
}
else if (nums[mid] < target) {
left = mid + 1;
}
else {
return mid;
}
}
return -1;
}
console.time("searchUsingLCRC");
searchUsingLCRC(nums, target1);
console.timeEnd("searchUsingLCRC");
console.time("searchUsingLCRO");
searchUsingLCRO(nums, target1);
console.timeEnd("searchUsingLCRO");
console.log(searchUsingLCRC(nums, target1));
console.log(searchUsingLCRO(nums, target1));
50 changes: 50 additions & 0 deletions0704-Binary_Search/main.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
const nums = [-1, 0, 3, 5, 9, 12]
const target1 = 9
const target2 = 2

function searchUsingLCRC(nums: number[], target: number): number {
let mid: number,
left: number = 0,
right: number = nums.length - 1

while (left <= right) {
mid = left + ((right - left) >> 1)
if (nums[mid] > target) {
right = mid - 1
} else if (nums[mid] < target) {
left = mid + 1
} else {
return mid
}
}

return -1
}

function searchUsingLCRO(nums: number[], target: number): number {
let mid: number,
left: number = 0,
right: number = nums.length
while (left < right) {
mid = left + ((right - left) >> 1)
if (nums[mid] > target) {
right = mid
} else if (nums[mid] < target) {
left = mid + 1
} else {
return mid
}
}
return -1
}

console.time("searchUsingLCRC")
searchUsingLCRC(nums, target1)
console.timeEnd("searchUsingLCRC")

console.time("searchUsingLCRO")
searchUsingLCRO(nums, target1)
console.timeEnd("searchUsingLCRO")

console.log(searchUsingLCRC(nums, target1))
console.log(searchUsingLCRO(nums, target1))
22 changes: 22 additions & 0 deletions0704-Binary_Search/readme.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
Given an array of integers `nums` which is sorted in ascending order, and an integer `target`, write a function to search `target` in `nums`. If `target` exists, then return its index. Otherwise, return `-1`.

You must write an algorithm with `O(log n)` runtime complexity.

Example 1:

> <span style="color: white;">Input</span>: nums = [-1,0,3,5,9,12], target = 9<br>
> <span style="color: white;">Output</span>: 4<br>
> <span style="color: white;">Explanation</span>: 9 exists in nums and its index is 4

Example 2:

> <span style="color: white;">Input</span>: nums = [-1,0,3,5,9,12], target = 2<br>
> <span style="color: white;">Output</span>: -1<br>
> <span style="color: white;">Explanation</span>: 2 does not exist in nums so return -1

Constraints:

- 1 <= nums.length <= 10<sup>4</sup>
- -10<sup>4</sup> < nums[i], target < 10<sup>4</sup>
- All the integers in `nums` are unique.
- `nums` is sorted in ascending order.

[8]ページ先頭

©2009-2025 Movatter.jp