当前位置:网站首页>【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;
}
}
边栏推荐
- Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
- MapReduce编程基础
- JS原型链
- Niuke monthly race 22 tree sub chain
- 闭包实现迭代器效果
- js变量提升(hoisting)
- Databinding source code analysis
- 美团2022年机试
- 2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
- ES6-const本质与完全不可改实现(Object.freeze)
猜你喜欢
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
【检测技术课案】简易数显电子秤的设计与制作
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
How to realize the usage of connecting multiple databases in idel
Principles of Microcomputer - internal and external structure of microprocessor
树结构---二叉树2非递归遍历
【电赛训练】红外光通信装置 2013年电赛真题
An overview of the design of royalties and service fees of mainstream NFT market platforms
OSPF - virtual link details (including configuration commands)
Implementation and application of queue
随机推荐
delete和delete[]引发的问题
[ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
Understand shallow replication and deep replication through code examples
Short circuit operator lazy evaluation
FAQ | FAQ for building applications for large screen devices
计网01-物理层
集成积木报表报错 org.apache.catalina.core.StandardContext.filterStart 启动过滤器异常
Key points of NFT supervision and overseas policies
Niuke monthly race 22- collect pieces of paper
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
Structure de l'arbre - - - arbre binaire 2 traversée non récursive
In the middle of the year, where should fixed asset management go?
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
JS prototype trap
【pytorch】nn.AdaptiveMaxPool2d
微信小程序 webview 禁止页面滚动,同时又不影响业务内overflow的滚动的实现方式
Pain points and solutions of equipment management in large factories
Learning practice: comprehensive application of cycle and branch structure (II)
树结构---二叉树1
MT7628K eCos开发入门