水王在哪

 1 package demo;
 2 import java.util.Stack;
 3 public class Test {
 4     public static void main(String[]args)
 5     {
 6 
 7         String[] a ={"a","b","a","a","a","c","b"};
 8         String shuiwang=select(a);
 9         System.out.println("水王是"+shuiwang);;
10     }
11 
12     static String select(String[] a)
13     {
14         Stack<String> s =new Stack<String>();
15         s.push("#");
16 
17         for(int i=0;i<a.length;i++)
18         {
19             if(a[i].equals(s.peek())||s.peek().equals("#"))
20             {
21                 s.push(a[i]);
22             }
23             else
24             {
25                 s.pop();
26             }
27         }
28         String rs=s.pop();
29         return rs;
30     }
31 }

 

posted @ 2017-04-17 19:44  不会编程的人  阅读(188)  评论(0编辑  收藏  举报