当前位置:网站首页>LeetCode34. 在排序数组中查找元素的第一个和最后一个位置
LeetCode34. 在排序数组中查找元素的第一个和最后一个位置
2022-06-12 07:27:00 【zhanghe_zht】
思路:二分查找
public class Solution {
public int[] searchRange(int[] nums, int target) {
int l = search2(nums, target);
int r = search2(nums, target + 1) - 1;
if (l == nums.length || nums[l] != target) {
return new int[]{
-1, -1};
}
return new int[]{
l, r};
}
public int search2(int[] nums, int target) {
int l = 0, r = nums.length;
while (l < r) {
int mid = (r + l) / 2;
if (nums[mid] >= target)
r = mid;
else
l = mid + 1;
}
return l;
}
}
边栏推荐
- Kotlin插件 kotlin-android-extensions
- Jackson XML is directly converted to JSON without writing entity classes manually
- Circular linked list and bidirectional linked list - practice after class
- The function of C language string Terminator
- LED lighting experiment with simulation software proteus
- New knowledge: monkey improved app crawler
- Non IID data and continuous learning processes in federated learning: a long road ahead
- Imx6q pwm3 modify duty cycle
- AcWing——4268. 性感素
- [Li Kou] curriculum series
猜你喜欢

Interview intelligence questions
![[yolo-v5 learning notes]](/img/f8/713210cafd7b750df540acbe03fd29.jpg)
[yolo-v5 learning notes]

Day 6 of pyhon

Knife4j first use

Scons编译IMGUI

Decryption game of private protocol: from secret text to plaintext

lambda 函数完美使用指南

Gd32f4 (5): gd32f450 clock is configured as 200m process analysis

Demonstrate "topic communication, action communication, service communication and parameter server" with a small turtle case

Detailed principle of 4.3-inch TFTLCD based on warship V3
随机推荐
五、EL 表达式& JSTL 标签库
Vscode outline preview cannot find file symbol
Detailed explanation of coordinate tracking of TF2 operation in ROS (example + code)
面试计算机网络-传输层
Lambda function perfect use guide
【WAX链游】发布一个免费开源的Alien Worlds【外星世界】脚本TLM
Node: cannot open /node: access denied
SSM integration
Design an open source continuous deployment pipeline based on requirements
Interview intelligence questions
Modelarts培训任务1
Why must coordinate transformations consist of publishers / subscribers of coordinate transformation information?
Use of gt911 capacitive touch screen
右击文件转圈卡住、刷新、白屏、闪退、桌面崩溃的通用解决方法
Freshmen are worried about whether to get a low salary of more than 10000 yuan from Huawei or a high salary of more than 20000 yuan from the Internet
C language sizeof strlen
D
循环链表和双向链表—课上课后练
Elegantly spliced XML
Map to sort