当前位置:网站首页>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;
}
};
边栏推荐
- 不会就坚持65天吧 只出现一次的数字
- 6. Pytest generates an allure Report
- 异常处理:pyemd或PyEMD找不到
- Differences and principles of bio, NiO and AIO
- C语言:结构体简单语法总结
- Won't you just stick to 62 days? Sum of words
- post导出数据,返回
- Down sampling and up sampling
- Beginner: array & String
- [k210 stepping pit] pytorch model is converted to kmodel and used on dock. (ultimately not achieved)
猜你喜欢
随机推荐
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
C language: talking about various complex statements
On quotation
Locker 2022.1.1
C language: summary of consortium knowledge points
12. Priority queue and inert queue
Multi rotor six axis hardware selection
Webrtc realizes simple audio and video call function
Leetcode 686. KMP method of repeatedly superimposing strings (implemented in C language)
WebRTC实现简单音视频通话功能
Common components of solder pad (2021.4.6)
Record the Niua packaging deployment project
C language: structure simple syntax summary
MySQL - deep parsing of MySQL index data structure
The principle of inverse Fourier transform (IFFT) in signal processing
Won't you just stick to 62 days? Sum of words
Why is it necessary to scale the attention before softmax (why divide by the square root of d_k)
post导出数据,返回
11.备份交换机
Not for 58 days. Implement prefix tree