当前位置:网站首页>【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;
}
}
边栏推荐
猜你喜欢

JS原型链

2.4 激活函数

Error org apache. catalina. core. StandardContext. FilterStart start filter exception
![2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data](/img/6e/d8ef618127ac492f5142f7b600266d.png)
2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data

How to solve the problem of fixed assets management and inventory?

Understanding and implementation of AVL tree

MapReduce编程基础

An overview of the design of royalties and service fees of mainstream NFT market platforms

MapReduce programming basics
![[interview brush 101] linked list](/img/52/d159bc66c0dbc44c1282a96cf6b2fd.png)
[interview brush 101] linked list
随机推荐
How to manage fixed assets efficiently in one stop?
Meituan machine test in 2022
A 419 error occurred in the laravel postman submission form. July 6th, 2020 diary.
MT7628K eCos开发入门
How to launch circle of friends marketing and wechat group activities
Preparing for the Blue Bridge Cup -- bit operation
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
Class loading
R language observation log (part24) -- initialization settings
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
PR training notes
美团2022年机试
[interview brush 101] linked list
ESP8266 FreeRTOS开发环境搭建
Niuke monthly race 22- collect pieces of paper
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
【pytorch】nn.AdaptiveMaxPool2d
Leetcode daily question brushing record --540 A single element in an ordered array
Introduction to mt7628k eCos development
Day06 branch structure and cycle (III)