-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
feat: Add Ruby code - chapter "Searching" #1262
Conversation
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.
Hi @bluebean-cloud, thanks for the PR, I have some comments
=end | ||
|
||
### 二分查找插入点(存在重复元素) ### | ||
def binary_search_insertion(nums, target) |
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.
Please import this method from the binary_search_insertion.rb
file
require_relative './binary_search_insertion'
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.
Thank you for reviewing!
If I use require_relative, then the driver Code in binary_search_insertion.rb
may be like:
### Driver Code ###
if __FILE__ == $0
# Code
end
Otherwise the driver code in binary_search_insertion.rb
will be executed when executing binary_serch_edge.rb
.
So may I make this change? @khoaxuantu
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.
Yes, it is. Actually, at the very first I did not expect the case that we will import another method from an index file. I will create another PR to put all the existing driver code at branch main into the __FILE__ == 0
block.
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.
OK. I will put my driver code into the __FILE__==0
block.
Thanks for suggestion!
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.
369f7e3 has fixed all bugs. thank you for the review!
### 方法一:暴力枚举 ### | ||
def two_sum_brute_force(nums, target) | ||
# 两层循环,时间复杂度为 O(n^2) | ||
for i in 0...nums.length |
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.
In the python code, the for loop is
for i in range(len(nums) - 1):
It traverse the nums
in range [0, nums.length - 1).
In your code, the loop traverses in range [0, nums.length). So please change to this to have a correct range:
suggestion~~ ~~for i in 0..nums.length~~ ~~
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.
0..nums.lenth seems to traverse the nums in [0, nums.length]. I will repair it, thank you!
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.
Oops! I got a mistake in the suggestion code. It should be
for i in 0...nums.length | |
for i in 0...(nums.length - 1) |
Sorry for the misinformation 😃
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.
I have made all the changes in commit
369f7e3
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.
LGTM
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.
Thanks for the PR!
If this pull request (PR) is associated with coding or code transpilation, please attach the relevant console outputs to the PR and complete the following checklist: