What is the error?


  • 0
    P
    public class Solution {
        public int strStr(String haystack, String needle) {
            if(haystack == null || needle == null)
               return -1;
            int nStackLen = haystack.length();
            int nNeedleLen = needle.length();
            if( nNeedleLen == 0) 
               return 0;
            int i=0, j=0, nret = -1;
            boolean isFirstRec = true;
            while(i<nStackLen && j < nNeedleLen)
            {
                if(haystack.charAt(i) == needle.charAt(j) )
                {
                    if(isFirstRec)
                    {
                       nret = i;
                       isFirstRec = false;
                    }
                    i++;
                    j++;
                }
                else if(nret != -1)
                {
                    nret = -1;
                    break;
                }
                else
                {
                    i++;
                }
            }
            return nret;
        }
    }

Log in to reply
 

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