本文主要是介绍Find Celebrity II,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
给你一个set<object> 然后给你了个compareTo的方法,可以比较出大小(任意两个objects都能比较出大小,但是这种关系不具有传递性,即A>B,B>C但是A不一定大于C),让你找出set中最大的object,如果不存在,就返回null。int compare(Object o1, Object o2) {return o1.toString().compareTo(o2.toString());}public Object findmax(LinkedList<Object> objs) {while (objs.size() > 1) { // remove 1 each roundObject o1 = objs.poll();Object o2 = objs.poll();int c = compare(o1, o2);if (c > 0) {objs.offer(o1);} else if (c < 0) {objs.offer(o2);} }if (objs.size() != 0) return objs.poll();return null;}public static void main(String[] args) {FindCelebrityII s = new FindCelebrityII();LinkedList<Object> objs = new LinkedList<>();objs.add("a");objs.add("c");objs.add("b");objs.add("d");System.out.println(s.findmax(objs));objs = new LinkedList<>();objs.add("a");objs.add("a");objs.add("a");objs.add("a");System.out.println(s.findmax(objs));}
这篇关于Find Celebrity II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!