当前位置:网站首页>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;
}
};
边栏推荐
- Niuke bm83 string deformation (case conversion, string inversion, string replacement)
- Some concepts about agile
- 4-33--4-35
- Mmdetection learning rate and batch_ Size relationship
- Several sentences extracted from the book "leather bag"
- Center and drag linked global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
- Vs+qt application development, set software icon icon
- 406. Reconstruct the queue according to height
- Plane vector addition
- Qt—绘制其他东西
猜你喜欢
【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》
B2020 分糖果
Implement Gobang with C language
Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
cpu飙升排查方法
[graphics] hair simulation in tressfx
Qt development - scrolling digital selector commonly used in embedded system
Série yolov5 (i) - - netron, un outil de visualisation de réseau
Dllexport and dllimport
随机推荐
My QT learning path -- how qdatetimeedit is empty
光猫超级账号密码、宽带账号密码 获取
7-3 rental (20 points)
从书本《皮囊》摘录的几个句子
Yolov5进阶之七目标追踪最新环境搭建(二)
Zzuli:1058 solving inequalities
牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
Address book sorting
C language STR function
Zzuli:1056 lucky numbers
Global and Chinese market of solder bars 2022-2028: Research Report on technology, participants, trends, market size and share
How to color ordinary landscape photos, PS tutorial
Zzuli: sum of 1051 square roots
【7.3】146. LRU caching mechanism
[opengl] face pinching system
Global and Chinese markets for infrared solutions (for industrial, civil, national defense and security applications) 2022-2028: Research Report on technology, participants, trends, market size and sh
Simulation of LS -al command in C language
[opengl] geometry shader
The latest M1 dedicated Au update Adobe audit CC 2021 Chinese direct installation version has solved the problems of M1 installation without flash back!
Yolov5 advanced seven target tracking latest environment construction (II)