设计思想:简单来讲 就是同归于尽思想,从头至尾依次遍历,每两个不相同ID消除,相同ID保留,最后剩余的ID就是水王
代码总结:
package ketang;
import java.util.Stack;
public class Waterking {
public static void main(String [] args){ String []a={"1","2","2","1","1","2","2"}; System.out.println(wk(a)); } public static String wk(String []a){ Stack<String> save =new Stack<String>(); save.push("#"); for(int i=0;i<a.length;i++) { if(a[i].equals(save.peek())||save.peek().equals("#")) { save.push(a[i]); } else { save.pop(); } } String rs=save.peek(); return rs; }}
代码截图:
个人总结: