当前位置:网站首页>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 0C++ 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;
}
};边栏推荐
- [graphics] efficient target deformation animation based on OpenGL es 3.0
- 零拷贝底层剖析
- Zhejiang University Edition "C language programming (4th Edition)" topic set reference ideas set
- Plane vector addition
- [opengl] face pinching system
- C # realizes the login interface, and the password asterisk is displayed (hide the input password)
- Global and Chinese market of transfer case 2022-2028: Research Report on technology, participants, trends, market size and share
- Center and drag linked global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
- Yolov5 advanced 8 format conversion between high and low versions
- Niuke bm83 string deformation (case conversion, string inversion, string replacement)
猜你喜欢
![[graphics] efficient target deformation animation based on OpenGL es 3.0](/img/53/852ac569c930bc419846ac209c8d47.jpg)
[graphics] efficient target deformation animation based on OpenGL es 3.0

cpu飙升排查方法

C language DUP function

4-20-4-23 concurrent server, TCP state transition;

Yolov5系列(一)——网络可视化工具netron

4-24--4-28

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系列(一)——網絡可視化工具netron

On MEM series functions of C language

Composite type (custom type)
随机推荐
Yolov5系列(一)——网络可视化工具netron
The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%
Detailed explanation of four modes of distributed transaction (Seata)
Time conversion ()
Tencent internship interview sorting
[ue4] cascading shadow CSM
Dllexport and dllimport
B2020 分糖果
[opengl] face pinching system
Zzuli:1043 max
Global and Chinese market of marketing automation 2022-2028: Research Report on technology, participants, trends, market size and share
[graphics] real shading in Unreal Engine 4
【注意力机制】【首篇ViT】DETR,End-to-End Object Detection with Transformers网络的主要组成是CNN和Transformer
[ue4] geometry drawing pipeline
Mmdetection learning rate and batch_ Size relationship
Global and Chinese markets for sterile packaging 2022-2028: Research Report on technology, participants, trends, market size and share
Adobe Premiere Pro 15.4 has been released. It natively supports Apple M1 and adds the function of speech to text
4-29——4.32
NOI OPENJUDGE 1.5(23)
Byte practice plane longitude 2