"Finding water king" of classroom exercises

Ideas: the array in cycles, if the number is 0, findid value assigned to the current cycle, if the number is 1, it is determined whether the value of findid same as the value of the current cycle, the same number plus one, or minus one, complete cycle of the left findid "water King."

package com.pre.one;
//20173522 李秦
public class Damot 
{

    public static void main(String[] args)
    {
        String []ids= { "aa","bb","aa","aa","cc","aa" ,"aa","ee","aa","hh","jj"};
        System.out.println(find(ids));
    }
    public static String find(String [] id)
    {
        if(id == null)
        {
            System.out.println("请输入id列表");
            return null;
        }
        int n=id.length;
        String findid=null;
        int cishu,i;
        for(i=cishu=0;i<n;i++)
        {
            if(cishu==0)
            {
                findid=id[i];
                cishu=1;
            }
            else
            {
                if(findid==id[i]) cishu++;
            
                else cishu--;
                    
            }
        }
        return findid;
    }
}

 

Guess you like

Origin www.cnblogs.com/lq13035130506/p/10951074.html