2017-04-08 26 views
-1

问题陈述:我必须从数组中删除n个副本。从整型数组中删除N个副本

以下是完整的问题陈述:https://pastebin.com/EJgKUGe3

,我的解决办法是:

public class minion_labour_shift_2ndTry { 
    static int[] data = {1,2, 2, 3, 3, 3, 4, 5, 5}; 

    public static void main(String[] args) { 

     Scanner reader = new Scanner(System.in); 
     int n = reader.nextInt(); 

     data = answer(data, n); 
     for (int i = 0; i < data.length; i++) { 
      System.out.print(data[i] + " "); 
     } 

    } 

    public static int[] answer(int[] data, int n) { 
     if (data.length>99){ 
      System.exit(0); 
     } 
     int[] result = new int[99]; 
     ArrayList<Integer> temp = new ArrayList<>(); 
     int counter = 0, count ,maxCount = 0; 

     for (int i = 0; i < data.length; i++) { 
      boolean isDistinct = false; 
      for (int j = 0; j < i; j++) { 
       if (data[i] == data[j]) { 
        isDistinct = true; 
        break; 
       } 
      } 
      if (!isDistinct) { 
       result[counter++] = data[i]; 
      } 
     } 
     for (int i = 0; i < counter; i++) { 
      count = 0; 
      for (int j = 0; j < data.length; j++) { 
       if (result[i] == data[j]) { 
        count++; 
       } 
      } 
      System.out.println("....... count"+count); 
      if (maxCount <= count){ 
       maxCount = count; 
      } 

      if (count <= n){ 
       temp.add(result[i]); 
      } 
     } 

     if (maxCount-1 < n){ 
      return data; 
     } 

     data = new int[temp.size()]; 
     for (int i = 0; i <temp.size() ; i++) { 
      data[i] = temp.get(i); 
     } 
     return data; 
    } 
} 

现在,我的问题是,什么我失踪,我应该做的通过所有10例什么。

感谢提前:)

NB:它会在Java 7中进行编译和地图,HashSet的或第三方库,输入/输出操作,产卵线程或进程,并转移到执行环境是不允许。

+0

'回答(数据,2)'返回'[1,2,4,5]',但应该返回'[1,2,2,4,5,5]'。 – Andreas

回答

0

我最初误读了要求,这确实是什么问:

public static int[] answer(int[] data, int n) { 
    Map<Integer, Integer> counts = new HashMap<>(); 
    int elementsNeeded = 0; 

    for (int i = 0; i < data.length; i++) { 
     Integer currentCount = counts.get(data[i]); 

     currentCount = currentCount == null ? 1 : ++currentCount; 
     counts.put(data[i], currentCount); 

     if (currentCount <= n + 1) { 
     elementsNeeded += currentCount > n ? -n : 1; 
     } 
    } 

    int[] resultArray = new int[elementsNeeded]; 
    int j = 0; 

    for (int i = 0; i < data.length; i++) { 
     if (counts.get(data[i]) <= n) { 
     resultArray[j++] = data[i]; 
     } 
    } 

    return resultArray; 
    } 

...而且自己的代码,稍微改动:

public static int[] answer2(int[] data, int n) { 
    if (data.length>99){ 
     System.exit(0); 
    } 
    ArrayList<Integer> temp = new ArrayList<>(); 
    int count; 

    for (int i = 0; i < data.length; i++) { 
     count = 0; 
     for (int j = 0; j < data.length; j++) { 
      if (data[i] == data[j]) { 
       count++; 
      } 
     } 

     if (count <= n){ 
      temp.add(data[i]); 
     } 
    } 


    data = new int[temp.size()]; 
    for (int i = 0; i <temp.size() ; i++) { 
     data[i] = temp.get(i); 
    } 
    return data; 
    } 
0

不会提供完整的解决方案,但建议对算法进行修改,因为它不清楚你在做什么,你从来没有解释过你对算法的实际想法。例如,你使用isDistinct来做什么?

1)循环一次并计算每个数字的频率。你可以使用一个长度为100的数组,因为这是所有的数据输入。当你循环时,跟踪两件事情:发生超过n次的条目总数,以及这些数字是哪些

2)创建一个合适大小(从上面计算)的结果数组和再次遍历列表并填写未超过阈值的元素。