java常见算法
1 去重 //去重复,需要额外定义一个List public static void RemoveRepeat(List arrs) { List tmp = new ArrayList(); Iterator it = arrs.iterator(); while (it.hasNext()) { int a = it.next(); if (tmp.contains(a)) it.remove() ; else tmp.add(a); } } 去不重 HashMap实现 public static void RemoveNotRepeat(List arrs) { Map tmp=new HashMap(); for(char c:arrs) { if(tmp.containsKey(c)) tmp.put(c,tmp.get(c)+1); else tmp.put(c,1); } Iterator it=arrs.iterator(); while (it.hasNext()) { Character c=it.next(); if(tmp.get(c)==1) it.remove(); } } 2 随机分配 ; else tmp.add(a); } } 去不重 HashMap实现 public static void RemoveNotRepeat(List arrs) { Map tmp=new HashMap(); for(char c:arrs) { if(tmp.containsKey(c)) tmp.put(c,tmp.get(c)+1); else tmp.put(c,1); } Iterator it=arrs.iterator(); while (it.hasNext()) { Character c=it.next(); if(tmp.get(c)==1) it.remove(); } } 2 随机分配
用户评论