Example: Given an ordered (ascending) integer array of n elements nums and a target value, write a function to search for the target in nums, or return - 1 if the target value has a return subscript.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 appears in nums with subscript 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Interpretation: 2 does not exist in nums and therefore returns - 1
Tips:
You can assume that all elements in nums are not duplicated.
n will be between [1,10,000].
Each element of nums will be between [-9999, 9999].
Source: LeetCode
Link: https://leetcode-cn.com/problems/binary-search
Copyright belongs to the seizure network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
class Solution { public: int search(vector<int>& nums, int target) { int begin=0,end=nums.size()-1; int mid; while(begin<=end){ mid=(begin+end)/2; if(target==nums[mid]) return mid; else if(target<nums[mid]){ end=mid-1; }else{ begin=mid+1; } } return -1; } };
1. Search insertion location
Given a sort array and a target value, find the target value in the array and return its index. If the target value does not exist in the array, return the position where it will be inserted sequentially.
You can assume that there are no duplicate elements in the array.
Example 1:
Input: [1, 3, 5, 6], 5
Output: 2
Example 2:
Input: [1, 3, 5, 6], 2
Output: 1
Example 3:
Input: [1, 3, 5, 6], 7
Output: 4
Example 4:
Input: [1, 3, 5, 6], 0
Output: 0
Source: LeetCode
Link: https://leetcode-cn.com/problems/search-insert-position
Copyright belongs to the seizure network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
class Solution { public: int searchInsert(vector<int>& nums, int target) { int begin=0,end=nums.size()-1; int mid; while(begin<=end){ mid=(begin+end)/2; if(target==nums[mid]) return mid; else if(target<nums[mid]){ end=mid-1; }else{ begin=mid+1; } } if(target<nums[mid]) return mid; else return mid+1; } };
2. Find the first and last positions of elements in a sorted array
Given an integer array nums in ascending order, and a target value. Find out the starting and ending positions of the given target values in the array.
The time complexity of your algorithm must be at the O(log n) level.
If there is no target value in the array, return [-1, -1].
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = 5, 7, 7, 8, 8, 10], target = 6
Output: [-1, -1]
Source: LeetCode
Link: https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array
Copyright belongs to the seizure network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { int begin=0,end=nums.size()-1; int mid; vector<int> pos(2,-1); pos[0]=bound(nums,target,0); pos[1]=bound(nums,target,1); return pos; } int bound(vector<int>& nums,int target,int b){ int begin=0,end=nums.size()-1; int mid; while(begin<=end){ mid=(begin+end)/2; if(target==nums[mid]){ if(b==0){ if(mid==0 || nums[mid-1]<target){ return mid; } end=mid-1; } if(b==1){ if(mid==nums.size()-1 || nums[mid+1]>target){ return mid; } begin=mid+1; } }else if(target<nums[mid]){ end=mid-1; }else{ begin=mid+1; } } return -1; } };
Suppose that the array sorted in ascending order rotates at some point that is unknown in advance.
(For example, an array [0, 1, 2, 4, 5, 6, 7] may become [4, 5, 6, 7, 0, 1, 2].
Search for a given target value, return its index if it exists in the array, or return - 1.
You can assume that there are no duplicate elements in the array.
The time complexity of your algorithm must be at the O(log n) level.
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
Source: LeetCode
Link: https://leetcode-cn.com/problems/search-in-rotated-sorted-array
Copyright belongs to the seizure network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
class Solution {
public:
int search(vector<int>& nums, int target) {
int begin=0,end=nums.size()-1,mid;
while(begin<=end){
mid=(begin+end)/2;
if(target==nums[mid]){
return mid;
}else{
if(nums[mid]>=nums[begin]){
if(target<nums[begin] || target>nums[mid]){
begin=mid+1;
}else{
end=mid-1;
}
}else{
if(target>nums[end] || target<nums[mid]){
end=mid-1;
}else{
begin=mid+1;
}
}
}
}
return -1;
}
};
4. Longest ascending subsequence
Given an unordered array of integers, find the length of the longest ascending subsequence (strictly ascending, not continuous).
Examples:
Input: [10, 9, 2, 5, 3, 7, 101, 18] Output: 4 Interpretation: The longest ascending subsequence is [2,3,7,101], and its length is 4.
Explain:
- There may be multiple combinations of the longest ascending subsequences, you just need to output the corresponding length.
- The time complexity of your algorithm should be O(n2).
Advancement: Can you reduce the time complexity of the algorithm to O(n log n)?
//Find the first greater than x Element subscripts, return start. int binary_search(int x, vector<int>& s) { int start = 0; int end = s.size() - 1; int mid; while (start<=end) { mid = (start + end) / 2; if (s[mid] > x) { end = mid - 1; } else if (s[mid] < x) { start = mid + 1; } else { return mid; } } return start; } class Solution{ public: int lengthOfLIS(vector<int>& nums) { int len=nums.size(); if(len==0) return 0; vector<int> stack; stack.push_back(nums[0]); int index=-1; for(int i=1;i<len;i++){ if(nums[i]>stack.back()){ stack.push_back(nums[i]); }else{ index=binary_search(nums[i],stack); stack[index]=nums[i]; } } return stack.size(); } };
If you look for the first element subscript less than x, return end