当前位置:网站首页>蓝桥杯练习020
蓝桥杯练习020
2022-04-22 16:26:00 【qq_2737035853】
蓝桥杯练习020
唯一成对的数
存在一个长度为 N 的数组,其内容为 1 ~ N - 1 连续自然数( N-1 个) 和 1 到 N - 1 中的某个数,即所谓数组中存在唯一成对的数。
此外,数据可能是乱序的。
目标是找出成对的数是什么。
举个栗子
举例来说,[2,3,1,2,4] 就是符合题目描述的数组,其中 2 是问题的解。
知识点
- 知识点 1:数组遍历
- 知识点 2:异或运算
实现思路
不用位运算
我们可以用标记法:
- 先开辟一个辅助数组
h,长度为N; - 遍历原数组
a,对h[a[i]]自增,即记录a[i]出现的次数; - 遍历数组
h,如果h[x]==2,x就是答案。
private static int solve0(int n, int[] arr) {
int[] helper = new int[n];
for (int i = 0; i < n; i++) {
helper[arr[i]]++;
}
for (int i = 0; i < n; i++) {
if (helper[i] == 2) {
return i;
}
}
return -1;
}
用位运算
我们可以换个角度来求解,即消除 1 ~ N - 1 ,那剩下的就是答案。此时能否直接对原数组用连续异或的方式呢?不行!直接用,会把成对的数消掉。
我们可以把 1 ~ N - 1 和原数组组合起来形成一个新数组,新数组中 1 ~ N - 1 每个数都出现 2 次,目标答案则会出现 3 次。只需对新数组遍历,连续做异或运算,最终结果就是答案。
当然,所谓新数组是为了便于读者理解,实际代码中无需开辟新数组。
private static int solve(int n, int[] arr) {
int x1 = 0;
for (int i = 1; i <= n - 1; i++) {
x1 = (x1 ^ i);
}
for (int i = 0; i < n; i++) {
x1 = x1 ^ arr[i];
}
return x1;
}
测试
假设把上述两种思路写在方法 solve0 和 solve1 中,可以用下面的 main 方法来输出两种思路的计算结果,看是否一致:
public static void main(String[] args) {
int N = 1001;
int[] arr = new int[N];
for (int i = 0; i < arr.length - 1; i++) {
arr[i] = i + 1;
}
//最后一个数,是随机数
arr[arr.length - 1] = new Random().nextInt(N - 1) + 1;
//随机下标
int index = new Random().nextInt(N);
swap(arr, index, arr.length - 1);
System.out.println(solve0(N, arr));
System.out.println("==========");
System.out.println(solve(N, arr));
}
/** * 在数组内原址交换元素 * * @param arr * @param i * @param j */
public static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
总结
相同的两个数异或后结果为 0 ,任何数和 0 异或后保持不变,这样的特性往往有妙用。
完整代码
import java.util.Random;
public class _01唯一成对的数 {
public static void main(String[] args) {
int N = 1001;
int[] arr = new int[N];
for (int i = 0; i < arr.length - 1; i++) {
arr[i] = i + 1;
}
//最后一个数,是随机数
arr[arr.length - 1] = new Random().nextInt(N - 1) + 1;
//随机下标
int index = new Random().nextInt(N);
swap(arr, index, arr.length - 1);
System.out.println(solve0(N, arr));
System.out.println("==========");
System.out.println(solve(N, arr));
}
private static int solve0(int n, int[] arr) {
int[] helper = new int[n];
for (int i = 0; i < n; i++) {
helper[arr[i]]++;
}
for (int i = 0; i < n; i++) {
if (helper[i] == 2) {
return i;
}
}
return -1;
}
private static int solve(int n, int[] arr) {
int x1 = 0;
for (int i = 1; i <= n - 1; i++) {
x1 = (x1 ^ i);
}
for (int i = 0; i < n; i++) {
x1 = x1 ^ arr[i];
}
return x1;
}
/** * 在数组内原址交换元素 * * @param arr * @param i * @param j */
public static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
版权声明
本文为[qq_2737035853]所创,转载请带上原文链接,感谢
https://lizhenkai.blog.csdn.net/article/details/123448395
边栏推荐
- 接口测试框架实战 | 流程封装与基于加密接口的测试用例设计
- Unittest-单元测试2
- 时间戳有什么作用,如何申请?
- Shiro customizes the cache and extends the Shiro cache module. You only need to configure the cache to realize session sharing
- 【ARM汇编】如何对键入数据做判断?
- Three dimensional model of power conductor
- selenium之预期条件判断方法
- C语言选择排序
- Servlet Foundation
- TCP / IP protocol IV TCP protocol (I) - Theory + practice to make it clear to you
猜你喜欢

It's too voluminous ~ (2022 version) large factory face experience + detailed notes to help you finish the interview

【csnote】范式

window10安装STEP7 Micro/Win V4.0 SP9,安装完后每次开机都提示Assertion Program:pniopcac.exe File

SolidWorks oblique reference plane

3D reconstruction of power conductor based on LIDAR point cloud

国美新动作“真选”“严选”赋能 多维度护航品质消费

国美零售借数字经济东风,打造“船身”的消费体验

Experiment 3 FFT and its application in convolution calculation and spectrum analysis

ASEMI低压降肖特基二极管比普通肖特基好在哪?

时代超群交流伺服驱动器设置
随机推荐
Insist on doing the right thing
Win10安装MySQL5.7(图文详解)
查询表中distinct 数据 数量 count mysql oracle 取指定条数
引入工程初始化
C language insertion sort
Color classification - brain sharp turn - training logic [stacking | single pointer | double pointer]
ICMP and IPv6 global unicast address dynamic allocation
这个API Hub厉害了,收录了钉钉企业微信等开放Api,还能直接调试 !
Niu Ke SQL question brushing record
【操作教程】国标GB28181平台EasyGBS如何开启语音对讲功能?
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
颜色分类--脑筋急转弯型--训练逻辑[堆排|单指针|双指针]
Times superior AC servo driver settings
Typescripts Promise
国美新动作“真选”“严选”赋能 多维度护航品质消费
Small exercise: binary search and Implementation
redis各个版本下载地址
引用拷贝,深拷贝,浅拷贝
Eltable style modification. Parent child components pass values. Fuzzy query
C语言选择排序