当前位置:网站首页>LeetCode(剑指 Offer)- 53 - I. 在排序数组中查找数字 I
LeetCode(剑指 Offer)- 53 - I. 在排序数组中查找数字 I
2022-07-29 04:25:00 【放羊的牧码】
题目链接:点击打开链接
题目大意:略
解题思路:略
相关企业
- 字节跳动
- 亚马逊(Amazon)
- 微软(Microsoft)
- 谷歌(Google)
- 苹果(Apple)
- 领英(LinkedIn)
- 彭博(Bloomberg)
AC 代码
- Java
// 解决方案(1)
class Solution {
public int search(int[] nums, int target) {
int from = find(nums, target - 0.1, target);
int to = find(nums, target + 0.1, target);
if (to < 0 || from > nums.length - 1) {
return 0;
}
return to - from - 1;
}
private int find(int[] nums, double num, int target) {
int left = 0;
int right = nums.length - 1;
int rs = -1;
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] > num) {
rs = right = mid - 1;
} else if (nums[mid] < num) {
rs = left = mid + 1;
} else {
return -1;
}
}
// 因为 rs 此时无论如何可能在目标值左边或右边, 而不是我们想当然的一定在目标值的(小于目标值)左边或(大于目标值)右边
// 所以需要修复一定让 rs 在目标值的(小于)左边或(大于)右边
if (rs == -1 || rs == nums.length) {
return rs;
} else if (num > target && nums[rs] < num) {
return rs + 1;
} else if (num < target && nums[rs] > num) {
return rs - 1;
}
return rs;
}
}
// 解决方案(2)
class Solution {
public int search(int[] nums, int target) {
return helper(nums, target) - helper(nums, target - 1);
}
int helper(int[] nums, int tar) {
int i = 0, j = nums.length - 1;
while(i <= j) {
int m = (i + j) / 2;
if(nums[m] <= tar) i = m + 1;
else j = m - 1;
}
return i;
}
}- C++
class Solution {
public:
int search(vector<int>& nums, int target) {
return helper(nums, target) - helper(nums, target - 1);
}
private:
int helper(vector<int>& nums, int tar) {
int i = 0, j = nums.size() - 1;
while(i <= j) {
int m = (i + j) / 2;
if(nums[m] <= tar) i = m + 1;
else j = m - 1;
}
return i;
}
};边栏推荐
- Two forms of softmax cross entropy + numpy implementation
- Realize the effect of univariate quadratic equation through JS. Enter the coefficients of a, B and C to calculate the values of X1 and x2
- Class starts! See how smardaten decomposes complex business scenarios
- JVM (heap and stack) memory allocation
- Star a pathfinding in LAYA
- Deploy Jenkins using containers
- Don't stick to it for 68 days. Baboons eat bananas
- Common components of solder pad (2021.4.6)
- The pit I walked through: the first ad Sketchpad
- Pytoch distributed training
猜你喜欢

Beginner: array & String

On quotation

不会就坚持64天吧 查找插入位置

不会就坚持62天吧 单词之和

14.haproxy+keepalived负载均衡和高可用

不会就坚持60天吧 神奇的字典

不会就坚持63天吧 最大的异或

On the use of pyscript (realizing office preview)

不会就坚持65天吧 只出现一次的数字

Hengxing Ketong invites you to the 24th China expressway informatization conference and technical product exhibition in Hunan
随机推荐
The pit I walked through: the first ad Sketchpad
不会就坚持63天吧 最大的异或
Pytoch automatic mixing accuracy (AMP) training
Multi card training in pytorch
不会就坚持69天吧 合并区间
TypeError: Cannot read properties of undefined (reading ‘then‘)
There are objections and puzzles about joinpoint in afterreturning notice (I hope someone will leave a message)
C语言力扣第61题之旋转链表。双端队列与构造循环链表
恒星科通邀您“湘”约第24届中国高速公路信息化大会暨技术产品展示会
你真的会写Restful API吗?
Realize the effect of univariate quadratic equation through JS. Enter the coefficients of a, B and C to calculate the values of X1 and x2
Common components of solder pad (2021.4.6)
i++与++i详解
Multi rotor six axis hardware selection
异常解决:cococaption包出现找不到edu.stanford.nlp.semgraph.semgrex.SemgrexPattern错误
C language: structure simple syntax summary
Star a pathfinding in LAYA
Whole house WiFi solution: mesh router networking and ac+ap
MySQL - clustered index and secondary index
12.优先级队列和惰性队列