在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
来源:互联网 发布:网络电视浏览器下载 编辑:程序博客网 时间:2024/05/16 11:47
public class Solution{
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of
// duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication
// in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications
// in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication) {
boolean flag=false;
boolean flag_duplication=true;
if(length <=1 || numbers==null){
return flag;
}
//使用基数排序
for(int i=0;i<numbers.length;i++){
while(numbers[i] !=i){
if(numbers[numbers[i]]==numbers[i]){
//找到相同的数字
duplication[0]=numbers[i];
flag=true;
return flag;
}
//交换
int temp=numbers[i];
numbers[i]=numbers[temp];
numbers[temp]=temp;
}
}
return flag;
}
}
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number,length of
// duplication array is 1,so using duplication[0] = ? in implementation;
// Here duplication like pointor in C/C++, duplication[0] equal *duplication
// in C/C++
// 这里要特别注意~返回任意重复的一个,赋值duplication[0]
// Return value: true if the input is valid, and there are some duplications
// in the array number
// otherwise false
public boolean duplicate(int numbers[],int length,int [] duplication) {
boolean flag=false;
boolean flag_duplication=true;
if(length <=1 || numbers==null){
return flag;
}
//使用基数排序
for(int i=0;i<numbers.length;i++){
while(numbers[i] !=i){
if(numbers[numbers[i]]==numbers[i]){
//找到相同的数字
duplication[0]=numbers[i];
flag=true;
return flag;
}
//交换
int temp=numbers[i];
numbers[i]=numbers[temp];
numbers[temp]=temp;
}
}
return flag;
}
}
阅读全文
0 0
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
- 在一个长度为n的数组里的所有数字都在1到n-1的范围内。 有一个数字重复若干次,找出这个数字。
- 数组中重复的数字(n个数字,且数字都在0到n-1范围内)
- 一个长度为N的整形数组,数组中每个元素的取值范围是[0,N-1],写一个算法判断数组中是否存在重复的数字
- 长度为n的数组求其中有几个长度为k的区间数字是连续的(无重复)
- 找出数组中重复的数字
- 找出数组中重复的数字
- 找出数组中重复的数字
- 找出数组中重复的数字
- 数组中重复的数字
- 数组中重复的数字
- 数组中重复的数字
- 数组中重复的数字
- 数组中重复的数字
- 数组中重复的数字
- 数组中重复的数字
- 定位
- 1139. First Contact (30) DFS
- 【Scikit-Learn 中文文档】把它们放在一起
- 数据结构-计算器点实现
- iOS 通过添加线程依赖和信号量结合实现一个复杂界面请求多个接口时按指定顺序执行
- 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
- Hadoop 搭建高可用完全分布式集群
- 已解决—Android 6.0 SDK中找不到HttpClient
- 彻底弄懂KMP
- loj6008「网络流 24 题」餐巾计划(费用流)
- UVA 11572 Unique Snowflakes
- 谈程序员的抽象思维
- 第一次打开这个博客仿佛进入一个神奇的地方
- python中标准的六个数据类型详解