当前位置:网站首页>287. 寻找重复数
287. 寻找重复数
2022-07-06 10:24:00 【ღCauchyོꦿ࿐】
题意

解一
- 二分答案
简单解释,官解也很详细。
定义: d p [ i ] {dp[i]} dp[i] 表示 n u m s [ i ] nums[i] nums[i] 中 < = i <=i <=i 的数的个数。
看这样一个序列:
nums:3 1 3 4 2
dp: 1 2 4 5
性质:自重复的数起,之后所有 d p [ i ] dp[i] dp[i] 都 > i >i >i ,之前都 < = i <=i <=i。
在看这样一个序列:
nums:3 1 3 4 3
dp: 1 1 4 5
尽管重复了多次,但是还是满足上面的性质。
那么我们二分答案,统计 < = m i d <=mid <=mid 的数的个数,如果个数 > m i d >mid >mid ,说明是在右侧,可能是答案, r = m i d r=mid r=mid,否则 l = m i d + 1 l=mid+1 l=mid+1。
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int n = nums.size();
int l = 1, r = n;
while(l < r) {
int mid = (l + r) >> 1;
auto f = [=](int x){
int res = 0;
for (int i = 0; i < n; i++)
if(nums[i] <= x) res += 1;
return res <= x;
};
if(f(mid)) l = mid + 1;
else r = mid;
}
return r;
}
};
/* 一旦加上了重复的数,那么之后一定更大 // 二分check,更小就不要,说明在答案左,l向右调整。 nums: 1 3 2 4 2 [1234]: 1 3 4 5 mid: 3 -> res = 4, r = 3 mid: 2 -> res = 3, r = 2 mid: 1 -> res = 1, l = 2 l == r = 2 nums: 1 3 2 3 4 3 [1234]: 1 2 5 6 mid: 3 -> res = 5, r = 3 mid: 2 -> res = 2, l = 3 l == r = 3 */
解二
二进制
重复一次:
- 重复的数为 x x x, [ 1 , n ] [1,n] [1,n] 都各出现一次,外加 x x x 多出现一次。
- 统计二进制下 1 1 1 的个数,原序列相比 [ 1 , n ] [1,n] [1,n] 序列,多出的 1 1 1 都是由 x x x 产生。
重复多次:
重复的数为 x x x, x x x 代替 [ 1 , n ] [1,n] [1,n] 中没出现的数 y y y,外加 x x x 多出现一次。
四种情况:00、01、10、11(以下x,y表示某一位为1或0)
- x = 0 , y = 0 x=0,y=0 x=0,y=0:<=
- x = 0 , y = 1 x=0,y=1 x=0,y=1:<=
- x = 1 , y = 0 x=1,y=0 x=1,y=0:>
- x = 1 , y = 1 x=1,y=1 x=1,y=1:>
所以,对于 > > > 的情况,将该位 1 1 1 加上即可。
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int n = nums.size();
int res = 0;
for (int i = 0; i < 31; i++) {
int x = 0, y = 0;
for (int j = 0; j < n; j++) {
if(nums[j] & (1 << i)) x += 1;
}
for (int j = 1; j < n; j++) {
if(j & (1 << i)) y += 1;
}
if(x > y) res |= (1 << i);
}
return res;
}
};
解三
- 快慢指针
n + 1 n + 1 n+1个数, [ 1 , n ] [1,n] [1,n] 范围内。每个位置 i i i 连边 i − i- i−> n u m s [ i ] nums[i] nums[i]。
3 1 3 4 2
0 1 2 3 4建边:0->3, 1->1, 2->3, 3->4, 4->2

明显重复的 3 3 3 是环的入口。
又点 0 0 0 只可能有出边,不可能自环。所以定义快慢指针 l , r l,r l,r 都从 0 0 0 开始。
然后就是环形链表找入口的常规解法:
- 慢一步、快二步
- 慢置为 0 0 0
- 慢一步、快一步
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int l = 0, r = 0;
do {
l = nums[l];
r = nums[nums[r]];
} while(l != r);
l = 0;
while(l != r) {
l = nums[l];
r = nums[r];
}
return l;
}
};
边栏推荐
- UDP protocol: simple because of good nature, it is inevitable to encounter "city can play"
- Declval (example of return value of guidance function)
- C语言自动预订飞机票问题
- 队列的实现
- Insert dial file of Jerry's watch [chapter]
- Codeforces Round #803 (Div. 2)
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- C语言高校实验室预约登记系统
- Olivetin can safely run shell commands on Web pages (Part 1)
- Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
猜你喜欢

Compilation principle - top-down analysis and recursive descent analysis construction (notes)

带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities

Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)

Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
![[swoole series 2.1] run the swoole first](/img/cd/88abf7e83e9d9d416051b33263690b.png)
[swoole series 2.1] run the swoole first

简单易用的PDF转SVG程序

Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day

编译原理——自上而下分析与递归下降分析构造(笔记)

F200 - UAV equipped with domestic open source flight control system based on Model Design

Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
随机推荐
[swoole series 2.1] run the swoole first
Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
C language college laboratory reservation registration system
Codeforces Round #803 (Div. 2)
Declval of template in generic programming
從交互模型中蒸餾知識!中科大&美團提出VIRT,兼具雙塔模型的效率和交互模型的性能,在文本匹配上實現性能和效率的平衡!...
Windows连接Linux上安装的Redis
Codeforces Round #803 (Div. 2)
STM32+ENC28J60+UIP协议栈实现WEB服务器示例
Jerry's updated equipment resource document [chapter]
2022暑期项目实训(一)
面试突击62:group by 有哪些注意事项?
简单易用的PDF转SVG程序
D binding function
OpenEuler 会长久吗
转载:基于深度学习的工业品组件缺陷检测技术
从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
【Android】Kotlin代码编写规范化文档
STM32按键状态机2——状态简化与增加长按功能
STM32+MFRC522完成IC卡号读取、密码修改、数据读写