当前位置:网站首页>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;
}
};边栏推荐
- PS tips - draw green earth with a brush
- 7-9 one way in, two ways out (25 points)
- Piwigo 2.7.1 sqli learning
- Tencent internship interview sorting
- Dllexport et dllimport
- There are links in the linked list. Can you walk three steps faster or slower
- 7-1 positive integer a+b (15 points)
- Optical cat super account password and broadband account password acquisition
- PHP GD image upload bypass
- Global and Chinese market of lighting control components 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

The picture quality has been improved! LR enhancement details_ Lightroom turns on AI photo detail enhancement: picture clarity increases by 30%

Composite type (custom type)

How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
![[opengl] advanced chapter of texture - principle of flowmap](/img/dd/6208122fcc578caaf098301b185e03.jpg)
[opengl] advanced chapter of texture - principle of flowmap

Solve the problem that PR cannot be installed on win10 system. Pr2021 version -premiere Pro 2021 official Chinese version installation tutorial

远程服务器后台挂起 nohup

B2020 points candy

PS tips - draw green earth with a brush

cpu飙升排查方法

C string format (decimal point retention / decimal conversion, etc.)
随机推荐
ASTC texture compression (adaptive scalable texture compression)
Global and Chinese market of solder bars 2022-2028: Research Report on technology, participants, trends, market size and share
dllexport和dllimport
On MEM series functions of C language
NOI OPENJUDGE 1.3(06)
Zzuli:1053 sine function
Zzuli:1054 monkeys eat peaches
Zzuli:1052 sum of sequence 4
5.4-5.5
How to color ordinary landscape photos, PS tutorial
NOI OPENJUDGE 1.5(23)
Zzuli: cumulative sum of 1050 factorials
406. 根据身高重建队列
Remote server background hangs nohup
Niuke bm83 string deformation (case conversion, string inversion, string replacement)
Vs+qt multithreading implementation -- run and movetothread
C language DUP function
Find books ()
406. Reconstruct the queue according to height
远程服务器后台挂起 nohup