·主要思路
·代码
import java.util.Stack; public class Water { public static void main(String[]args) { String[] a ={"1","1","1","1","2","2","2"}; String waterwang=select(a); System.out.println("水王:"); System.out.println(waterwang); } static String select(String[] a) { Stack<String> s =new Stack<String>(); s.push("#"); for(int i=0;i<a.length;i++) { if(a[i].equals(s.peek())||s.peek().equals("#")) { s.push(a[i]); } else { s.pop(); } } String rs=s.pop(); return rs; } }
·结果截图