当前位置:网站首页>287. finding duplicates
287. finding duplicates
2022-06-09 06:51:00 【XiuXiu's wonderful journey】

- violence : Two layers of for loop
Speed pointer



class Solution {
public int findDuplicate(int[] nums) {
int slow = 0;
int fast = 0;
slow = nums[slow];
fast = nums[nums[fast]];
// fast Move back two steps ,slow Move a step , Find the meeting node
// This movement doesn't understand what's going on
while(slow != fast){
// namely
slow = nums[slow];
fast = nums[nums[fast]];
}
// Find the entry node
int pre1 = 0;
int pre2 = slow;
while(pre1 != pre2){
pre1 = nums[pre1];
pre2 = nums[pre2];
}
return pre1;
}
}
Two points search

class Solution {
public int findDuplicate(int[] nums) {
int n = nums.length;
int l = 1, r = n - 1, ans = -1;
while (l <= r) {
// Keep cutting the array , change mid Value
int mid = (l + r) >> 1;
int cnt = 0;
// Less than... In the statistics array mid The number of
for (int i = 0; i < n; ++i) {
if (nums[i] <= mid) {
cnt++;
}
}
// Quantity ratio mid Small , So the front ones are all in line with , So change left Value
if (cnt <= mid) {
l = mid + 1;
// Quantity ratio mid many , Change right right Value , By the way, the current mid Assign to ans
} else {
r = mid - 1;
ans = mid;
}
}
return ans;
}
}
边栏推荐
- Chapter_ 03 mask operation of matrix
- Risc-v foundation and Hummingbird e203 learning summary
- 不懂数学可以使用机器学习编程吗?
- UML series articles (27) architecture modeling - Deployment
- Camtasia 2022发布更新功能介绍
- After ppt imports video clipping, how to save the clipped video as?
- UML series articles (21) high level behavior - events and signals
- 实现轮播图就是这么简单!!!
- cms 和 g1的主要區別
- Mockito no return value method and exception mock
猜你喜欢

数据库操作语句

UML series articles (27) architecture modeling - Deployment

修改IDEA格式化单行注释 后增加空格

Qt控制台工程显示Label小窗口
新同事上来就把项目性能优化了一遍,瑟瑟发抖...

Jump from one pit to another

UML系列文章(19)基本行为---交互图

Quanzhi v3s learning record (12) use of rtl8723bs

DS_ How to solve the problem of files automatically generated by the store under the folder?

UML系列文章(26)体系结构建模---制品
随机推荐
mockito 无返回值方法及异常mock
Quanzhi v3s learning record (11) audio and video Usage Summary
BSP tailoring of Quanzhi platform (7) rootfs tailoring -- tailoring of user tools and Libraries & rootfs compression
MySQL 5.7 installation tutorial (full step, nanny level tutorial)
BSP tailoring (2) attachment 1 General setup configuration description
多线程基础(二)
[raspberry pie 4B deep learning garbage classification] Chap.3 raspberry pie installs opencv and tests the garbage classification of the real-time video stream of the video interface [deep learning mo
Can I use machine learning programming without knowing mathematics?
MySQL of quantitative transactions - MySQL database events
新手,之前买了个理财产品,怎么看收益啊?
BSP tailoring (6) Attachment III - rootfs menuconfig configuration description
关于用户消息的推送
[raspberry pie 4B deep learning garbage classification] Chap.1 configure the raspberry pie with keras+tensorflow [deep learning moves]
你真的搞明白了 Dart 中两个对象相等的逻辑了吗?
clickhouse2分片2副本高可用集群搭建及chproxy代理配置使用
UML series articles (25) high level behavior - state diagram
UML系列文章(27)體系結構建模---部署
UML系列文章(26)体系结构建模---制品
What should Parker hydraulic motor pay attention to?
【SDU项目实训2019级】个人主页页面展示+个人总结