-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create 0034-find-first-and-last-position-of-element-in-sorted-array.js #2620
Conversation
Solved find-first-and-last-position-of-element-in-sorted-array in js.
if(target < nums[mid]) { | ||
right = mid-1; | ||
} | ||
// this is the meat of the code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Remove comment and creat descriptive variables or functions
const isTarget = (...);
if (isTarget) { ... }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
result.push(binarySearch(true)); | ||
result.push(binarySearch(false)); | ||
|
||
function binarySearch(leftBias) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: name booleans that prompt a question
isLeftBias
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a kind reminder that I updated the code as suggested.
Making variable names more descriptive.
result.push(binarySearch(true)); | ||
result.push(binarySearch(false)); | ||
|
||
function binarySearch(isLeftBias) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid nested functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
|
||
while(left <= right) { | ||
|
||
const mid = Math.floor((left+right)/2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use bitwise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Using the bitwise operator and removing the inner function.
Solved find-first-and-last-position-of-element-in-sorted-array in js.
File(s) Added: 0034-find-first-and-last-position-of-element-in-sorted-array.js
Language(s) Used: JavaScript
Submission URL: https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/submissions/985379429/