当前位置:网站首页>【leetcode】287. Find duplicates
【leetcode】287. Find duplicates
2022-07-01 09:25:00 【Chinese fir sauce_】
subject :
287. Look for repetitions
Given an inclusion n + 1 Array of integers nums , The numbers are all in [1, n] Within the scope of ( Include 1 and n), We know that there is at least one duplicate integer .
hypothesis nums Only A repeated integer , return The number of repetitions .
The solution you design must Don't modify Array nums And only constant level O(1) Extra space .
Example 1:
Input :nums = [1,3,4,2,2]
Output :2
Example 2:
Input :nums = [3,1,3,4,2]
Output :3
Tips :
1 <= n <= 105
nums.length == n + 1
1 <= nums[i] <= n
nums in There's only one whole number appear Two or more times , Other integers only appear once
Dichotomy :
It can be analyzed in combination with drawer principle .
class Solution {
public int findDuplicate(int[] nums) {
// Use dichotomy
/* Less than O(n^2) Two points search Let's not think about arrays , Just think about it The numbers are all there 1 To n Between Example 1: arr = [1,3,4,2,2] At this time, the number is in 1 — 5 Between mid = (1 + 5) / 2 = 3 arr Less than or equal to 3 Yes 4 individual (1,2,2,3),1 To 3 There must be duplicate values in mid = (1 + 3) / 2 = 2 arr Less than or equal to 2 Yes 3 individual (1,2,2),1 To 2 There must be duplicate values in mid = (1 + 2) / 2 = 1 arr Less than or equal to 1 Yes 1 individual (1),2 To 2 There must be duplicate values in So the number of repetitions is 2 */
int low = 0, high = nums.length - 1, mid = -1, count;
while ( low < high ) {
// Guess the middle point is repeated , Find the number less than or equal to the middle number , If it is equal to the middle number, it means there is no repetition , Move up the interval , Otherwise, the interval has repetition number
mid = (low + high) / 2;
count = 0;
for (int num : nums) {
if(num <= mid) {
count ++;
}
}
// If the number of values of the number is less than or equal to the number , Then slide up the interval
if ( count <= mid ) {
low = mid + 1;
} else {
// The number of occurrences is greater than this value , Then down
high = mid;
}
}
return low;
}
}
边栏推荐
- Naoqi robot summary 28
- Set the type of the input tag to number, and remove the up and down arrows
- 【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
- Flink interview questions
- 【pytorch学习】torch.device
- Football and basketball game score live broadcast platform source code /app development and construction project
- Learning practice: comprehensive application of cycle and branch structure (II)
- Design and manufacture of simple digital display electronic scale
- 序列化、监听、自定义注解
- delete和delete[]引发的问题
猜你喜欢
随机推荐
2.2 【pytorch】torchvision. transforms
[pytorch] 2.4 convolution function nn conv2d
Microcomputer principle - bus and its formation
js原型陷阱
Can diffusion models be regarded as an autoencoder?
nacos簡易實現負載均衡
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
Jetson nano installs tensorflow GPU and problem solving
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + MQ Series + nodejs local service + MySQL storage
樹結構---二叉樹2非遞歸遍曆
树结构---二叉树2非递归遍历
ES6-const本质与完全不可改实现(Object.freeze)
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
Construction of esp8266 FreeRTOS development environment
Set the type of the input tag to number, and remove the up and down arrows
序列化、监听、自定义注解
手指点击屏幕就模拟进入F11进入全屏
Flink interview questions
NiO zero copy








