当前位置:网站首页>Leetcode sword offer find the number I (nine) in the sorted array
Leetcode sword offer find the number I (nine) in the sorted array
2022-07-03 14:59:00 【& eternal Galaxy &】
Title Description
Count the number of times a number appears in the sort array .
Example 1:
Input : nums = [5,7,7,8,8,10], target = 8 Output : 2
Example 2:
Input : nums = [5,7,7,8,8,10], target = 6 Output : 0
Python3 Code implementation
Realization way : Two points search
class Solution:
def search(self, nums: List[int], target: int) -> int:
return self.binary_search(nums, 0, len(nums)-1, target)
def binary_search(self, nums, low, high, target):
if low <= high:
mid = low + (high - low) // 2
if nums[mid] == target:
return self.binary_search(nums, low, mid-1, target) + self.binary_search(nums, mid+1, high, target) + 1
elif nums[mid] > target:
return self.binary_search(nums, low, mid-1, target)
else:
return self.binary_search(nums, mid+1, high, target)
return 0
C++ Code implementation
Realization way : Two points search
class Solution {
public:
int search(vector<int>& nums, int target) {
return binary_search(nums, 0, nums.size()-1, target);
}
public:
int binary_search(vector<int>& nums, int low, int high, int target){
if(low <= high){
int mid = static_cast<int>(low + (high - low) / 2);
if(nums[mid] == target){
return binary_search(nums, low, mid-1, target) + binary_search(nums, mid+1, high, target) + 1;
}
else if(nums[mid] > target){
return binary_search(nums, low, mid-1, target);
}
else{
return binary_search(nums, mid+1, high, target);
}
}
return 0;
}
};
边栏推荐
- 分布式事务(Seata) 四大模式详解
- 牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
- Yolov5系列(一)——网络可视化工具netron
- Several sentences extracted from the book "leather bag"
- 2022/02/14
- Yolov5 series (I) -- network visualization tool netron
- C string format (decimal point retention / decimal conversion, etc.)
- Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
- There are links in the linked list. Can you walk three steps faster or slower
- Piwigo 2.7.1 sqli learning
猜你喜欢
PS tips - draw green earth with a brush
Open under vs2019 UI file QT designer flash back problem
Yolov5系列(一)——網絡可視化工具netron
Bucket sorting in C language
To improve efficiency or increase costs, how should developers understand pair programming?
Vs+qt multithreading implementation -- run and movetothread
C language to realize mine sweeping
[ue4] geometry drawing pipeline
C language fcntl function
复合类型(自定义类型)
随机推荐
Yolov5 advanced seven target tracking latest environment construction (II)
Série yolov5 (i) - - netron, un outil de visualisation de réseau
1017 a divided by B (20 points)
dllexport和dllimport
Vs+qt application development, set software icon icon
Yolov5 advanced 8 format conversion between high and low versions
7-3 count the number of words in a line of text
How does vs+qt set the software version copyright, obtain the software version and display the version number?
Yolov5 series (I) -- network visualization tool netron
Dllexport and dllimport
Yolov5进阶之七目标追踪最新环境搭建(二)
Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
B2020 分糖果
QT program font becomes larger on computers with different resolutions, overflowing controls
My QT learning path -- how qdatetimeedit is empty
Zzuli:1040 sum of sequence 1
4-29——4.32
Niuke bm83 string deformation (case conversion, string inversion, string replacement)
Zzuli:1059 highest score
Yolov5进阶之八 高低版本格式转换问题