猿辅导java第一题这么做有没有问题啊?
package test1; import java.util.*; public class test1 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); Map arr=new LinkedHashMap<Integer,Integer>(); List l = new ArrayList<Integer>(); for(int i = 0;i<n;i++){ int key = in.nextInt(); if(l.contains(key)){ continue; }else if(!arr.containsKey(key)){ arr.put(key,1); }else if(arr.containsKey(key) && (int)arr.get(key) < m){ int num = (int)arr.get(key); arr.put(key,num+1); }else if(arr.containsKey(key) && (int)arr.get(key) >= m){ arr.remove(key); l.add(key); } } Iterator iter = arr.entrySet().iterator(); while(iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); for(int i = 0;i<(int)entry.getValue();i++){ System.out.print(entry.getKey() + " "); } } } }
快交卷了,没测,大佬们看看,这样的输出有问题吗?会不会有顺序问题啥的?我咋感觉做复杂了。