剑指offer--数组中重复的数字
来源:互联网 发布:js bind 原理 编辑:程序博客网 时间:2024/05/29 17:01
题目描述
别人的做法:
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
public class 数组中重复的数字开始 {public static void main(String[] args) {// TODO Auto-generated method stub//int[] numbers = {2,3,1,0,2,5,3};int[] numbers = {};int length =numbers.length;int[] duplication = new int[1];System.out.println(duplicate(numbers, length, duplication)+":"+duplication[0]);} public static boolean duplicate(int numbers[],int length,int [] duplication) { if (length!=numbers.length||numbers.length<2||numbers==null) { duplication[0] = -1;return false;} int[] copynum = new int[length]; for (int i = 0; i < copynum.length; i++) { copynum[i] = -1; if (numbers[i]>length-1) {return false;}} for (int i = 0; i < numbers.length; i++) {if (copynum[numbers[i]]==-1) {copynum[numbers[i]]=1;}else {copynum[numbers[i]]++;}} for (int i = 0; i < copynum.length; i++) {if (copynum[i]>1) {duplication[0]=i;return true;}}return false; }}
别人的做法:
public boolean duplicate(int numbers[], int length, int[] duplication) { boolean[] k = new boolean[length]; for (int i = 0; i < k.length; i++) { if (k[numbers[i]] == true) { duplication[0] = numbers[i]; return true; } k[numbers[i]] = true; } return false; }
阅读全文
0 0
- 剑指offer--数组中重复的数字
- 《剑指offer》数组中重复的数字
- 剑指offer:数组中重复的数字
- [剑指Offer]数组中重复的数字
- 剑指offer:数组中重复的数字
- 剑指offer 数组中重复的数字
- [剑指offer]数组中重复的数字
- 剑指offer|数组中重复的数字
- 剑指offer-数组中重复的数字
- 剑指offer 数组中重复的数字
- 《剑指offer》数组中重复的数字
- 剑指offer-数组中重复的数字
- 剑指offer--数组中重复的数字
- 剑指offer--数组中重复的数字
- 剑指offer:数组中重复的数字
- 剑指offer:数组中重复的数字
- 剑指offer:数组中重复的数字
- 【剑指offer】数组中重复的数字
- R || 聊天记录分析(二)
- 使用过的小巧软件一览
- 抽象类和接口
- 爬取安卓手机应用内容
- debian stretch + kernel 4.4 固件发布(支持硬件加速),可安装kodi
- 剑指offer--数组中重复的数字
- Android 内存优化总结&实践
- ZZULIOJ【1120】最值交换【数组】
- TSUNG学习教程 安装与使用(二)
- UML类图知识简介
- mysql和oracle的区别(功能性能、选择、使用它们时的sql等对比)
- D
- Lucas定理(板子+理解
- hdu--6121--Build a tree