当前位置:网站首页>2022-07-17:1, 2, 3... N-1, N, n+1, n+2... In this sequence, only one number has repetition (n). This sequence is unordered. Find the repeated number n. This sequence is ordered. Find the repeated numb
2022-07-17:1, 2, 3... N-1, N, n+1, n+2... In this sequence, only one number has repetition (n). This sequence is unordered. Find the repeated number n. This sequence is ordered. Find the repeated numb
2022-07-27 00:23:00 【Fuda frame constructor daily question】
2022-07-17:1、2、3…n-1、n、n、n+1、n+2…
In this sequence , Only one number repeats (n).
This sequence is disordered , Find duplicate numbers n.
This sequence is ordered , Find duplicate numbers n.
answer 2022-07-17:
Cannot use hash table .
First question , The two methods , Fast and slow pointer ring finding problem and XOR method .
Second questions , Dichotomy .
The code to use rust To write . The code is as follows :
use rand::Rng;
use std::collections::HashSet;
fn main() {
let nn: i32 = 10;
let test_time: i32 = 3000;
println!(" Beginning of the test ");
for _ in 0..test_time {
let mut arr = random_array(rand::thread_rng().gen_range(0, nn) + 1);
let mut arr2 = arr.clone();
if right(&mut arr) != find_duplicate(&mut arr) {
println!(" Error in unsorted condition ! Fast and slow pointer mode ");
break;
}
if right(&mut arr2) != find_duplicate2(&mut arr2) {
println!(" Error in unsorted condition ! Exclusive or mode ");
break;
}
arr.sort();
if right(&mut arr) != find_duplicate_sorted(&mut arr) {
println!(" Error in sorting ! Dichotomy ");
break;
}
}
println!(" End of test ");
}
// In order to test
// Absolutely right , But traverse directly + Hashtable , There is no way to score
fn right(arr: &mut Vec<i32>) -> i32 {
let mut set: HashSet<i32> = HashSet::new();
for num in arr.iter() {
if set.contains(num) {
return *num;
}
set.insert(*num);
}
return -1;
}
// Meet the requirements of the topic 、 Unordered array , Find duplicates
// Time complexity O(N), Extra space complexity O(1)
// Use the speed pointer
fn find_duplicate(arr: &mut Vec<i32>) -> i32 {
if arr.len() < 2 {
return -1;
}
let mut slow = arr[0];
let mut fast = arr[arr[0] as usize];
while slow != fast {
slow = arr[slow as usize];
fast = arr[arr[fast as usize] as usize];
}
// slow == fast
fast = 0;
while slow != fast {
fast = arr[fast as usize];
slow = arr[slow as usize];
}
// Meet again ! One conclusion
return slow;
}
// Meet the requirements of the topic 、 Unordered array , Find duplicates
// Time complexity O(N), Extra space complexity O(1)
// Using XOR
fn find_duplicate2(arr: &mut Vec<i32>) -> i32 {
if arr.len() < 2 {
return -1;
}
let mut ans = 0;
for i in 0..arr.len() as i32 {
ans ^= i;
}
for i in 0..arr.len() as i32 {
ans ^= arr[i as usize];
}
// Meet again ! One conclusion
return ans;
}
// Meet the requirements of the topic 、 Ordered array , Find duplicates
// Time complexity O(logN), Extra space complexity O(1)
fn find_duplicate_sorted(arr: &mut Vec<i32>) -> i32 {
if arr.len() < 2 {
return -1;
}
let mut l: i32 = 0;
let mut r: i32 = arr.len() as i32 - 1;
let mut m: i32;
let mut ans = -1;
while l <= r {
m = (l + r) / 2;
if (m - 1 >= 0 && arr[(m - 1) as usize] == arr[m as usize])
|| (m + 1 < arr.len() as i32 && arr[(m + 1) as usize] == arr[m as usize])
{
ans = arr[m as usize];
break;
}
if m - l == arr[m as usize] - arr[l as usize] {
l = m + 1;
} else {
r = m - 1;
}
}
return ans;
}
// In order to test
fn random_array(n: i32) -> Vec<i32> {
let mut ans: Vec<i32> = vec![];
for i in 0..n + 1 {
ans.push(i + 1);
}
ans[n as usize] = rand::thread_rng().gen_range(0, n) + 1;
let mut i = n;
while i > 0 {
let j = rand::thread_rng().gen_range(0, i + 1);
let tmp = ans[i as usize];
ans[i as usize] = ans[j as usize];
ans[j as usize] = tmp;
i -= 1;
}
return ans;
}
The results are as follows :

边栏推荐
- Method of realizing program startup and self startup through registry
- Anaconda => PyCharm => CUDA => cudnn => PyTorch 环境配置
- 在pycharm中部署yolov5报错问题
- LeetCode——哈希表篇
- Skiasharp's WPF self drawn bouncing ball (case version)
- Deep learning of parameter adjustment skills
- Mysql8 installation
- deeplabcut使用1
- 数据库:MySQL基础+CRUD基本操作
- LeetCode题目——二叉树篇
猜你喜欢

13_集成学习和随机森林(Ensemble Learning and Random Forests)

Complete review of parsing web pages

Apple TV HD with the first generation Siri remote is listed as obsolete

3 esp8266 nodemcu network server

Arthas quick start

Anaconda = > pycharm=> CUDA=> cudnn=> pytorch environment configuration

第2章 开发用户流量拦截器

画冲击函数

Chapter 1 develop the first restful application

LeetCode——链表篇
随机推荐
20220720折腾deeplabcut2
Database: MySQL foundation +crud basic operation
Relationship between Unicode and UTF-8
Transformers is a graph neural network
Practice of data storage scheme in distributed system
类与对象笔记一
13_集成学习和随机森林(Ensemble Learning and Random Forests)
Configure deeplobcut 1 with your head covered
"Could not load host key" error when xshell connects to the server
[Gorm] model relationship -hasone
Codeforces D. two divisors (number theory, linear sieve)
10_ Name Case - Calculation attribute
LeetCode——哈希表篇
C and pointer Chapter 18 runtime efficiency 18.3 runtime efficiency
Complete backpack and 01 Backpack
Apple TV HD with the first generation Siri remote is listed as obsolete
Mysql database complex operations: Database Constraints, query / connect table operations
4. Talk about the famous Zhang Zhengyou calibration method
机器人学台大林教授课程笔记
C and pointers Chapter 18 runtime environment 18.8 programming exercises