一个面试算法题   大家进来看看!

maria728 2007-06-14 06:23:51
小弟昨天面试遇到的  请大家指教!


写一个长度为n 的整型数组 , 求出重复出现次数最多的那个数 .
...全文
287 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
沙漠8266 2008-05-24
  • 打赏
  • 举报
回复
发现一楼的代码有点问题 补充了一下,测试可以得到正确的结果了。
package com.interFace;

public class SearchFor {

public static void main(String[] args)
{

int[] array = new int[] {2, 1,1,1,1,1,1,1,1,1,1, 3, 4, 5, 5, 4, 5, 3, 6, 5, 3, 2, 5, 7, 5,7,7};
int result = 0;
int maxvalue = 0;
int j = 0;
for(int i = 0; i < array.length; i++)
{
int c = search(array, array[i]);
int next = (i == (array.length - 1 )) ? array.length - 1 : i + 1;
int n = search(array, array[next]);
result = c > n ? array[i] : array[next];
if(c>n){
if(maxvalue>c){

}else{
maxvalue = c;
j = array[i];
}
}else{
if(maxvalue>c){

}else{
maxvalue = c;
j = array[next];
}
}
}
System.out.println( "重复最多的数字是: " + j);
}

public static int search(int[] array, int w)
{
int counter = 0;
for (int i = 0; i < array.length; i++)
{
if (w == array[i])
{
counter++;
}
}
return counter;
}

}
winthesky 2007-06-15
  • 打赏
  • 举报
回复
更正:

public class Test5 {

public void gogogo(int[] array) {
int result = 0;
int temp = 0;
for (int i = 0; i < array.length; i++) {
int c = search(array, array[i]);
if (c > temp) {
temp = c;
result = array[i];
}
}
System.out.println("重复最多的数字是: " + result);
}

private int search(int[] array, int w) {
int counter = 0;
for (int i = 0; i < array.length; i++) {
if (w == array[i]) {
counter++;
}
}
return counter;
}

public static void main(String[] args) {
Test5 t = new Test5();
int[] array = new int[] {1, 2, 2, 2, 8, 2, 3, 4, 5, 5, 6, 7, 8};
t.gogogo(array);
}
}
justinavril 2007-06-14
  • 打赏
  • 举报
回复
public class Search {
public Search(){

}

public int find(int[] array, int i){
int countTemp = 0;

for(int j=0; j<array.length; j++){
if(array[i] == array[j]){
countTemp ++;
}
}
return countTemp;
}

public static void main(String args[]){
int[] array = new int[]{2,2,2,3,3,3,1,1,1};
Search app = new Search();
int result = 0;
int max = 0;

for(int i=0; i<array.length; i++){
if(result < app.find(array, i)){
result = app.find(array, i);
max = array[i];
}
}
System.out.println("出现最多的数是:" + max + "\n出现的次数是:" + result);
}
}

楼上最好把循环条件里的9改成num.length;
不过好像如果同时出现好几个数字重复次数一样的话,就郁闷了,只能打印其中的一个~~
moon1219 2007-06-14
  • 打赏
  • 举报
回复
class Test
{
public static void main(String[] args)
{
System.out.println("Hello World!");
int num[]=new int[]{1,2,3,2,2,2,4,5,6,7};
int i,j,aa=0,k=0,max=1;
for(i=0;i<9;i++)
{ k=0;
for(j=0;j<9;j++)
{
if(num[i]==num[j])
{
k++;
if(k>max)
{max=k;
aa=num[i];
}
}
}
}
System.out.println("the number is: "+aa+": "+max+"times");
}
}
justinavril 2007-06-14
  • 打赏
  • 举报
回复
沙发的结果有误 输出的总是最后一个数。
winthesky 2007-06-14
  • 打赏
  • 举报
回复
result = c > n ? array[i] : array[next];
这句是把出现次数最多的一个数赋给变量 result
winthesky 2007-06-14
  • 打赏
  • 举报
回复
int next = i == array.length - 1 ? array.length - 1 : i + 1;
这句是判断是否已经循环到数组的倒数第二个,以至于不会出现数组越界的错误.
maria728 2007-06-14
  • 打赏
  • 举报
回复
厉害  

在结帐前  能解释一下吗? 小弟对算法不是很明白

谢谢! 



int next = i == array.length - 1 ? array.length - 1 : i + 1;

result = c > n ? array[i] : array[next];
kingboy0310 2007-06-14
  • 打赏
  • 举报
回复
为了你能更好的学习,我就不写程序代码了.只写个思路
程序思路:
设计循环,让数组中的数依次与里面的数比较,
并把每个数的比较总数存入一个变量,这个数也存另一个变量.
到下个数比较完成后做与这两个变量的比较,大于就覆盖写入,小于就跳过.
循环结束后就是最多次数的数字与重复次数!
winthesky 2007-06-14
  • 打赏
  • 举报
回复
献丑了。。

大概这样:

public class Test5 {

public void gogogo(int[] array) {
int result = 0;
for (int i = 0; i < array.length; i++) {
int c = search(array, array[i]);
int next = i == array.length - 1 ? array.length - 1 : i + 1;
int n = search(array, array[next]);
result = c > n ? array[i] : array[next];
}
System.out.println("重复最多的数字是: " + result);
}

private int search(int[] array, int w) {
int counter = 0;
for (int i = 0; i < array.length; i++) {
if (w == array[i]) {
counter++;
}
}
return counter;
}

public static void main(String[] args) {
Test5 t = new Test5();
int[] array = new int[] {1, 2, 3, 4, 5, 5, 4, 5, 3, 6, 5, 3, 2, 5, 7, 5};
t.gogogo(array);
}
}

62,623

社区成员

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

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