The termination conditions about the binary search
2016-11-04 11:09
453 查看
About some thoughts of binary search:
To avoid some confusion of the binary search, like dead-loop or something.
Every time i write binary search, i always use the 'while' condition like this:
int l = 0;
int r = nums.size()-1;
while (l < r-1) {
// insert the specific code
}
And then to check both the nums[l] and nums[r] with the final result.
This way could definitely eliminate the possibility of dead-loop, like 'l' always be the 'mid', and 'mid' always equals to 'l'.
But this way is not very elegant, so, when could we use the condition like:
while (l<r) {
// insert the code here
}
To do like this way, we need have condition like:
while (l < r) {
int mid = l + (r - l) / 2;
if (citations[mid] >= size-mid) r = mid;
else l = mid + 1; // The key
}
So if, l = mid + 1, then we could use while (l < r).
Like the following problem: https://leetcode.com/problems/h-index-ii/
Both solution can reach the correct answer:
// First Edition
class Solution {
public:
int hIndex(vector<int>& citations) {
if ((int)citations.size() == 0) return 0;
int size = (int)citations.size();
int l = 0;
int r = size - 1;
while (l < r-1) {
int mid = l + (r - l) / 2;
if (citations[mid] >= size-mid) r = mid;
else l = mid + 1;
}
if (citations[l] >= size - l) return size-l;
else if (citations[r] >= size - r) return size-r;
return 0;
}
};
// We can also write like this
class Solution {
public:
int hIndex(vector<int>& citations) {
if ((int)citations.size() == 0) return 0;
int size = (int)citations.size();
int l = 0;
int r = size - 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (citations[mid] >= size-mid) r = mid;
else l = mid + 1;
}
if (citations[r] != 0)
return size-r;
return 0;
}
};
To avoid some confusion of the binary search, like dead-loop or something.
Every time i write binary search, i always use the 'while' condition like this:
int l = 0;
int r = nums.size()-1;
while (l < r-1) {
// insert the specific code
}
And then to check both the nums[l] and nums[r] with the final result.
This way could definitely eliminate the possibility of dead-loop, like 'l' always be the 'mid', and 'mid' always equals to 'l'.
But this way is not very elegant, so, when could we use the condition like:
while (l<r) {
// insert the code here
}
To do like this way, we need have condition like:
while (l < r) {
int mid = l + (r - l) / 2;
if (citations[mid] >= size-mid) r = mid;
else l = mid + 1; // The key
}
So if, l = mid + 1, then we could use while (l < r).
Like the following problem: https://leetcode.com/problems/h-index-ii/
Both solution can reach the correct answer:
// First Edition
class Solution {
public:
int hIndex(vector<int>& citations) {
if ((int)citations.size() == 0) return 0;
int size = (int)citations.size();
int l = 0;
int r = size - 1;
while (l < r-1) {
int mid = l + (r - l) / 2;
if (citations[mid] >= size-mid) r = mid;
else l = mid + 1;
}
if (citations[l] >= size - l) return size-l;
else if (citations[r] >= size - r) return size-r;
return 0;
}
};
// We can also write like this
class Solution {
public:
int hIndex(vector<int>& citations) {
if ((int)citations.size() == 0) return 0;
int size = (int)citations.size();
int l = 0;
int r = size - 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (citations[mid] >= size-mid) r = mid;
else l = mid + 1;
}
if (citations[r] != 0)
return size-r;
return 0;
}
};
相关文章推荐
- Tricks in decide the index in the binary search with duplicate elements
- Find the second largest element in a Binary Search Tree
- List Information About the Binary Files Used by an Application
- the latest update validate Binary Search leetcode c++
- What are the open source code search engines you know about
- Something about Binary search tree
- What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?
- Compute the height of a binary search tree
- Find the nearest common ancestor of any two nodes in a binary search tree
- List Information About the Binary Files Used by an Application
- all the questions about binary trees
- @the meaning of life@ about search machine
- Find the maximum by binary search
- The binary search of distributed programming翻译
- List Information About the Binary Files Used by an Application
- [geeks]Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST
- A class that implements the Set interface using a binary search tree
- Search the Nth element of Level M in a binary tree
- Binary search for the first element greater than target
- The bubble sort and Binary search