当前位置:网站首页>[leetcode] sword finger offer 53 - I. find the number I in the sorted array
[leetcode] sword finger offer 53 - I. find the number I in the sorted array
2022-07-02 10:14:00 【Chinese fir sauce_】
subject :
The finger of the sword Offer 53 - I. Look up numbers in the sort array I
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
Tips :
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums It is a group of non decreasing numbers
-109 <= target <= 109
Ideas : First, determine the left and right boundaries by dichotomy , Then look from left to right .
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
int l = 0,r = n - 1;
int count = 0;
while (l <= r) {
int mid = (l + r) / 2;
// Determine the right boundary
if(nums[mid] >= target) {
r = mid - 1;
}
// Determine left boundary
else {
l = mid + 1;
}
}
for( ;l < n && target == nums[l++];){
count++;
}
return count;
}
}
- Time complexity :O(n)
- Spatial complexity :O(1)
边栏推荐
- Commutateur Multi - lentilles Blender
- [Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology
- go语言入门
- How to achieve the top progress bar effect in background management projects
- [200 Shengxin literatures] 95 multiomics exploration TNBC
- Data insertion in C language
- Blender多镜头(多机位)切换
- This monitoring system makes workers tremble: turnover intention and fishing can be monitored. After the dispute, the product page has 404
- 阿里云ack介绍
- Tinyxml2 reading and modifying files
猜你喜欢
ue虛幻引擎程序化植物生成器設置——如何快速生成大片森林
2837xd 代码生成——补充(2)
Illusion -- Animation blueprint, state machine production, character walking, running and jumping action
Junit4 runs MVN test test suite upgrade scheme
Vs+qt set application icon
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
Alibaba cloud SMS service
2837xd code generation - Supplement (2)
Brief analysis of edgedb architecture
2837xd代码生成模块学习(1)——GPIO模块
随机推荐
阿里云Prometheus监控服务
滲透測試的介紹和防範
Error reporting on the first day of work (error reporting when Nessus installs WinPcap)
[ue5] blueprint making simple mine tutorial
[illusory] automatic door blueprint notes
[Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology
Bugkuctf-web24 (problem solving ideas and steps)
Project practice, redis cluster technology learning (VIII)
2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?
MySQL default transaction isolation level and row lock
Bugkuctf-web21 (detailed problem solving ideas and steps)
Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
2837xd 代码生成——StateFlow(3)
Minimum number of C language
2837xd 代码生成——补充(3)
[ue5] animation redirection: how to import magic tower characters into the game
MySQL transaction
Centos7 one click compilation and installation of PHP script
2837xd code generation - Supplement (3)