i am trying to post my solution..and please help me reduce some unnecessary codes.please...


  • 0
    S
    public int strStr(String haystack, String needle) {
                char [] src=haystack.toCharArray();
                char [] sub=needle.toCharArray();
                if(haystack.length()<needle.length())
                    return -1;
                if(haystack.length()==0||needle.length()==0)
                    return 0;
                int index=0;
                int rank=0;
                for(int i=0;i<haystack.length();i++){
                      if(rank>0&&i-rank>index)
                   {
                       rank=0;
                       i=index+1;
                       index=0;
                   }
                    if(rank<needle.length()&&src[i]==sub[rank])
                    {
                        if(rank==0)
                            index=i;
                            rank++;
                         if(rank==needle.length())
                           break;
                    }
                }
                if(rank!=needle.length())
                    index=-1;
            return index;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.