我是否正确理解了这种递归?

时间:2015-05-01 21:37:16

标签: java recursion

有人可以帮助我完成我的递归代码吗?这是我理解它的方式(但我认为我没有正确地介绍代码):

  1. if ( first > last ) return -1
  2. 否则
  3. if ( result == 0 ) return last
  4. else return SeqSearch (data, first, last-1, key)
  5. 重启方法但lastlast-1(“keller”)
  6. 重复步骤1,2和3
  7. else return SeqSearch(data, first, last-1, key)
  8. 重启方法,但lastlast-1(“6”)
  9. 等...
  10. 这是我的代码:

    public static void main (String[] args)
    {
        String[] data = new String[]{"help","jackson","six","keller","mean"};
        int first = 0;
        int last = data.length-1;
        String key ="help";
        System.out.println(SeqSearch(data,first,last,key));
    }
    public static int SeqSearch(String[] data,int first,int last,String key)
    {
        if(first > last)
            return -1;
        else{
            int result = data[last].compareTo(key);
            if(result == 0)
                return last;
            else
                return SeqSearch(data,first,last-1,key);
        }
    }
    

0 个答案:

没有答案