当前位置:网站首页>【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;
}
}
边栏推荐
- Tree structure --- binary tree 1
- js重写自己的函数
- Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
- js原型继承仅可继承实例而非构造器
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
- Construction of esp8266 FreeRTOS development environment
- tensorrt yolov5_ trt. Py comments
- JS原型链
- Naoqi robot summary 28
- 微信小程序 webview 禁止页面滚动,同时又不影响业务内overflow的滚动的实现方式
猜你喜欢
2.2 【pytorch】torchvision.transforms
JS scope chain and closure
js作用域链与闭包
Imitation of Baidu search results top navigation bar effect
nacos簡易實現負載均衡
Redis -- lattice connects to redis cluster
【pytorch】softmax函数
[video game training] real topic of 2013 video game of infrared optical communication device
JS prototype chain
Principle and application of single chip microcomputer timer, serial communication and interrupt system
随机推荐
nacos服务配置和持久化配置
Log4j 日志框架
Summary of reptile knowledge points
Principle and application of single chip microcomputer timer, serial communication and interrupt system
2.4 激活函数
Leetcode daily question brushing record --540 A single element in an ordered array
MySQL optimization
Set the type of the input tag to number, and remove the up and down arrows
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
队列的实现和应用
树结构---二叉树2非递归遍历
Rich text interpolation
How to effectively align team cognition
手指点击屏幕就模拟进入F11进入全屏
Wechat applet WebView prohibits page scrolling without affecting the implementation of overflow scrolling in the business
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
[interview brush 101] linked list
How to realize the usage of connecting multiple databases in idel
[pytorch learning] torch device