[面试题]去掉一个已经排好序的数组的重复数字,速度尽量快

老紫竹 2008-11-09 02:20:09
加精
提问:JAVA去掉一个已经排好序的数组的重复数字,速度尽量快,注意: 已经排序好的.



题目来源是.NET的的一个面试题:http://topic.csdn.net/u/20081103/14/023e43dd-8241-4928-83f4-6db022a3ee47.html
...全文
11327 348 打赏 收藏 转发到动态 举报
写回复
用AI写文章
348 条回复
切换为时间正序
请发表友善的回复…
发表回复
JavaScript_R 2012-10-19
  • 打赏
  • 举报
回复
int[] oNums ={1,2,2,3,3,3,4,4,4,4,5,5,5,5,5};
final int LENGTH = oNums.length;
int[] nums2 = new int[LENGTH];
int l=0;
int n=0;
String str="";
for(int i:oNums){
if(n<i){
n=i;
nums2[l]=n;
l++;
}
}
yjflinchong 2012-10-16
  • 打赏
  • 举报
回复
期待楼主答案
aleijie 2012-07-24
  • 打赏
  • 举报
回复
public static void main(String[] args) {
int[] oNums ={1,2,2,3,3,3,4,4,4,4,5,5,5,5,6};
int ol = oNums.length;
int[] o = new int[ol];
int lei = 0;
o[lei] = oNums[lei];
for(int i = 0;i<(ol-1);i++){
if(oNums[i]<oNums[i+1]){
lei++;
o[lei] = oNums[i+1];
}
}
for(int i = 0 ;i<o.length ; i++){
System.out.println(o[i]);
}
}
humyna 2012-05-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

把数组给set容器,它能去重复项,然后在给回数组
[/Quote]

+1
伤心书生 2012-04-26
  • 打赏
  • 举报
回复
新手 来偷学的 嘿嘿
xuxinqiujiao123 2012-04-21
  • 打赏
  • 举报
回复
[Quote=引用 87 楼 的回复:]
本面试官为大家解答本题,这道题并不是考察面试者的算法多高,而是要看出你对算法的技巧。很多人都在考虑负荷去掉重复数字这个问题,而经验老道的人就会特别在意“已经排好序的”这个条件。

既然已经排好序了,那么就设置一个序列变量(0),只要比我大我就把你存起来,然后我等于你,一个循环下来很快的解决问题了。

public class Test
{
public static void ……
[/Quote]
我的想法也是两个数组来解决。但是我在想能不能写个通用的程序,而非单独对哪个呢···
xutao650 2012-03-09
  • 打赏
  • 举报
回复
public class Test2{
public static void main(String[] args){
int nums[] = {1,2,2,3,3,4,4,6,6,6,6,7,8,9};
int samecount=1;
int count = 0;
int k=0;
int j=0;
while(j<nums.length){
if(j!=nums.length-1){
for(int i=j;i<nums.length-1;i++){
if(nums[j] == nums[i+1]){
samecount++;
}
}
nums[j+1] = nums[j+samecount];
System.out.print(nums[j]+" ");
j=j+samecount;
samecount = 1;
count++;
}else{
System.out.print(nums[j]);
j++;
}
}
}
}
小氓 2012-02-09
  • 打赏
  • 举报
回复
这么多笨蛋啊 ?用list还让你写什么算法啊
小氓 2012-02-09
  • 打赏
  • 举报
回复
稍微修改下:
public static void distinctInt(int[] a){
System.out.print(a[0]+ " ");
if(a.length>1){
for(int i=1;i<a.length;i++){
if(a[i]==a[i-1]){
continue;
}else{
System.out.print(a[i]+ " ");
}
}
}
}
小氓 2012-02-09
  • 打赏
  • 举报
回复
个人觉得最快的:
public static void distinctInt(int[] a){
System.out.print(a[0]+ " ");
for(int i=1;i<a.length;i++){
if(a[i]==a[i-1]){
continue;
}else{
System.out.print(a[i]+ " ");
}
}
}
我就改个名 2012-02-08
  • 打赏
  • 举报
回复
这个题 用list装的就是对的 赶紧结贴把 没意思了 这么多人回答
niceplay 2011-11-28
  • 打赏
  • 举报
回复
把楼主的arrayList换成linkedList是不是会更好一点呢?
arrayList查询快,linkedList插入快
蔡鸟 2011-11-27
  • 打赏
  • 举报
回复
一个贱招,代码最少。。
int array={2,23,423,4,2,3,4,23,4,2,34,2,3,4,23,4,23,4,234,2};
TreeMap<Integer,Integer> ts=new TreeMap<Integer,Integer>();
int length=array.length;
for(int i=0;i<length;i++)
ts.put(Integer.valueOf(array[i]),Integer,valueOf(array[i]));
够短吧。

楼主的这题是有一个很快的方法啦,
其实只要能利用已排序,相同在某一段来进行检查,用一变量定位某一值的开始。直到历遍到下一个不同值的index。
排除相同就已经是最快了。呵呵
pepsl6686022 2011-11-22
  • 打赏
  • 举报
回复
学习!~~~~~~~~~~~~~~
haotainan 2011-11-08
  • 打赏
  • 举报
回复
public class Sample
{
public static void main(String[] args)
{
int[] array = {1,2,2,3, 3, 5, 6, 7, 8, 8, 8, 10, 12, 13, 13, 15, 20};
int[] counnt=new int[array.length];

int i = 0;

while(i<array.length-1)
{
if(array[i]!=array[i+1]){

System.out.println(array[i+1]);
}
i++;
}
System.out.println(array[0]);
}
}
这个也可以,满足题目要求
wang_nai_jun 2011-11-03
  • 打赏
  • 举报
回复
个人感觉和#269楼相同的几位同行是面试官最想看到的结果。
(1)代码最简单,内存最节省,方法定义准确。
(2)和实现语言无关。也就是不要用语言相关的类库。

zly880818 2011-10-12
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<malloc.h>
#include<memory.h>


int eliminate_repeated(int array[],int ** new_array,int count)
{
int i = 0;
int j = 0;

if(NULL == array || NULL == new_array)
{
return -1;
}

for(i=0;i<count;i++)
{
if(i == 0)
{
*(*new_array+j) = array[i];
j++;
}
else
{
if(array[i] != array[i-1])
{
*(*new_array+j) = array[i];
j++;
}
}
}
return 0;
}


void main()
{
int old_array[8] = {0,0,1,2,2,3,3,4};
int *new_array = NULL;
int char_count = 1;
int i = 0;
int j=0;
int pre_char = 0;
int ret = 0;

for(i=0;i<8;i++)
{
if(old_array[i] != pre_char)
{
char_count ++;
}
pre_char = old_array[i];
}

new_array = (int *)malloc(sizeof(int)*char_count);
memset(new_array,0,sizeof(int)*char_count);

ret = eliminate_repeated(old_array,&new_array,8);

if(-1 == ret)
{
return;
}

for(i=0;i<char_count;i++)
{
printf("new_array[%d]=%d\n",i,new_array[i]);
}

}
懒得搭理你 2011-09-30
  • 打赏
  • 举报
回复
看我的。。。
package snt.wyb.test;

public class TestString {

/**
* JAVA去掉一个已经排好序的数组的重复数字,速度尽量快
* 注意: 已经排序好的
*/
public static void main(String[] args) {
int[] array = {1,1,2,2,3,3,3,4,4,5,9,9,9,9,12,12,12,12,12,15};
boolean[] newArr = new boolean[array[array.length-1]+1];
for(int i=0;i<array.length;i++){
newArr[array[i]] = true;
}
for(int i=0;i<newArr.length;i++){
if(newArr[i]){
System.out.print(i+" ");
}
}


}

}
涛涛_2009 2011-09-29
  • 打赏
  • 举报
回复
期待中……
wan423016 2011-05-01
  • 打赏
  • 举报
回复
public class test7 {

public static void main(String[] args)
{
int[] a = {1, 2, 2, 3, 4, 5, 5, 5, 7, 7, 8, 9, 9, 23, 45, 75, 75, 78, 80, 99, 99, 199};


int [] b=new int[a.length];
int s=1,t=1;
int i=0;
b[0]=a[0];
while(i<a.length&&t<a.length)
{
if(a[i]!=a[t])
{
b[s]=a[t];
s++;
i=t;
}
t++;
}
for(int j=0;j<b.length;j++){
if(b[j]!=0)
System.out.print(b[j]+" ");
}
}
}
献丑一下
加载更多回复(321)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧