当前位置:网站首页>Sword finger offer 03 Duplicate number in array
Sword finger offer 03 Duplicate number in array
2022-06-25 15:36:00 【anieoo】
Original link : The finger of the sword Offer 03. Repeated numbers in an array

solution:
The hash table stores the numbers that have occurred , Time complexity O(n)
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
unordered_set<int> map;
for(auto &x : nums) {
if(map.count(x)) return x;
map.insert(x);
}
return 0;
}
};Sort + Traverse , Time complexity O(nlogn)
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
for(int i = 1;i < nums.size();i++) {
if(nums[i] == nums[i - 1]) return nums[i];
}
return 0;
}
};Given element must be in 0~n - 1, If the number of occurrences is 1, It must be able to switch to the correct position .
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
for(int i = 0;i < nums.size();i++) {
while(nums[i] != i) {
if(nums[nums[i]] == nums[i]) return nums[i];
swap(nums[nums[i]], nums[i]);
}
}
return 0;
}
};边栏推荐
- Talk about the creation process of JVM objects
- Leetcode123 timing of buying and selling stocks III
- Simulating Sir disease transmission model with netlogo
- google_ Breakpad crash detection
- Cross compilation correlation of curl Library
- (1) Introduction
- Principle and implementation of MySQL master-slave replication (docker Implementation)
- Using R language in jupyter notebook
- Kali modify IP address
- Client development (electron) data store
猜你喜欢

(1) Introduction

Yolov5 Lite: fewer parameters, higher accuracy and faster detection speed

Some usage records about using pyqt5

程序员 VS 黑客的思维 | 每日趣闻

解决Visio和office365安装兼容问题

剑指 Offer 03. 数组中重复的数字

Data feature analysis skills - correlation test

Brief object memory layout
![[paper notes] semi supervised object detection (ssod)](/img/18/9fba70e6e4329722d9c6ad51d15724.jpg)
[paper notes] semi supervised object detection (ssod)

剑指 Offer 06. 从尾到头打印链表
随机推荐
Image segmentation based on deep learning: network structure design
Data feature analysis skills - correlation test
Principle and implementation of MySQL master-slave replication (docker Implementation)
[paper notes] contextual transformer networks for visual recognition
2. operator and expression multiple choice questions
Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detection
Esp8266 building smart home system
Is it safe to open a stock account through the account opening link of the account manager?
Boost listening port server
55 specific ways to improve program design (2)
Solution of push code failure in idea
Advertising effect cluster analysis (kmeans)
3. Sequential structure multiple choice questions
Could not connect to redis at 127.0.0.1:6379 in Windows
剑指 Offer 04. 二维数组中的查找
剑指 Offer 10- I. 斐波那契数列
Graphic control and layout basis of R visualization
Postman usage notes, interface framework notes
MySQL performance optimization - index optimization
Brief introduction to class loading process