当前位置:网站首页>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;
}
};
边栏推荐
- Vs+qt multithreading implementation -- run and movetothread
- 什么是Label encoding?one-hot encoding ,label encoding两种编码该如何区分和使用?
- Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
- NOI OPENJUDGE 1.4(15)
- My QT learning path -- how qdatetimeedit is empty
- [graphics] hair simulation in tressfx
- Zzuli:1048 factorial table
- Zzuli: sum of 1051 square roots
- 从书本《皮囊》摘录的几个句子
- 牛客 BM83 字符串變形(大小寫轉換,字符串反轉,字符串替換)
猜你喜欢
Qt—绘制其他东西
C language to realize mine sweeping
4-29——4.32
QT - draw something else
Use of form text box (I) select text
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
Implement Gobang with C language
Detailed explanation of four modes of distributed transaction (Seata)
Rasterization: a practical implementation (2)
随机推荐
Web server code parsing - thread pool
Find books ()
【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》
链表有环,快慢指针走3步可以吗
Zhejiang University Edition "C language programming (4th Edition)" topic set reference ideas set
Several sentences extracted from the book "leather bag"
[opengl] pre bake using computational shaders
Implement Gobang with C language
Vs+qt multithreading implementation -- run and movetothread
【微信小程序】WXSS 模板样式
Zzuli:1059 highest score
Zzuli:1055 rabbit reproduction
Yolov5系列(一)——網絡可視化工具netron
Pytorch深度学习和目标检测实战笔记
Troubleshooting method of CPU surge
Série yolov5 (i) - - netron, un outil de visualisation de réseau
Introduction to opengl4.0 tutorial computing shaders
Bucket sorting in C language
【Transform】【实践】使用Pytorch的torch.nn.MultiheadAttention来实现self-attention
B2020 points candy