水王

package demo;

import java.util.Stack;

public class Test {

    public static void main(String[]args)

    {

 

        String[] a ={"a","b","a","a","a","c","b"};

        String shuiwang=select(a);

        System.out.println("水王是"+shuiwang);;

    }

 

    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;

    }

}

 

 

 

截图:

 

 

package demo;

import java.util.Stack;

public class Test {

    public static void main(String[]args)

    {

 

        String[] a ={"a","b","a","a","a","c","b"};

        String shuiwang=select(a);

        System.out.println("水王是"+shuiwang);;

    }

 

    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;

    }

}

 

 

 

截图:

 

 

 

posted @ 2017-04-17 22:35  Joker明哥  阅读(229)  评论(0编辑  收藏  举报