数组中重复的数字(O(1))
来源:互联网 发布:u型耳夹淘宝 编辑:程序博客网 时间:2024/06/07 23:54
import java.util.Arrays;import java.util.Scanner;/** * 题:数组中重复的数字 * 要求:长度为n的数组里每个数都在0~n-1的范围内。数组中某些数字重复,但是不知道哪几个数字重复, * 也不知道重复了几次。找出数组中任意一个重复的数字 * 复杂度为O(1) ** Created by misfortune on 2017/10/26. */public class dulicate { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int wait[] = new int[n]; for (int i = 0; i < n; i++) { wait[i] = sc.nextInt(); } Arrays.sort(wait);//O(nlogn) operate(wait); sc.close(); for(int i=0;i<n;i++){ System.out.print(wait[i]+" "); } } public static void operate(int[] wait) { int n=wait.length,temp; if(n>2){ for(int i=0;i<n;i++){ if(wait[i]!=i){ if(wait[wait[i]]==wait[i]){ System.out.print(wait[i]+"重复!"); }else{ temp=wait[i]; wait[i]=wait[temp]; wait[temp]=temp; } } } } }}
阅读全文
0 0
- 数组中重复的数字(O(1))
- 数组中重复的数字(剑指offer)+时间复杂度O(n)空间复杂度O(1)的想法
- 数组中重复的数字(数组)
- 返回数组中任意某个重复的数字--时间复杂度O(n)空间复杂度O(1)
- 数组中重复的数字(没理解)
- 剑指offer(53):数组中重复的数字
- 47.数组中重复的数字(快速排序)
- 剑指offer(47)-数组中重复的数字
- (去重)去除数组中重复数字的算法
- 数组中重复的数字(Java实现)
- 数组中重复的数字(java版)
- (剑指offer)数组中任意一个重复的数字
- 数组中重复的数字
- 数组中重复的数字
- 数组中重复的数字
- 数组中重复的数字
- 数组中重复的数字
- 数组中重复的数字
- [bzoj3990][SDOI2015]排序 搜索
- Linux 查看端口占用网速方法
- 每日一练-20171026
- 顺序栈的定义和基本算法
- <四>Linux文件权限与属性详解--chattr & lsattr
- 数组中重复的数字(O(1))
- 消息机制简单实现模块间解耦
- 基于redis sentinel 的tomcat7 集群 session 共享
- 2017开学训练第九周周中总结
- 在集群上运行Spark
- python学习记录:列表
- springSecurity自定义用户登录页面时,常见的问题
- HttpServletRequest 中 getRequestURL和getRequestURI的区别
- Maven使用tomcat8-maven-plugin插件