Monday, June 5, 2017

strStr

二刷
class Solution {
    /**
     * Returns a index to the first occurrence of target in source,
     * or -1  if target is not part of source.
     * @param source string to be scanned.
     * @param target string containing the sequence of characters to match.
     */
    public int strStr(String source, String target) {
        // write your code here
        if (source == null || target == null || source.length() < target.length()) return -1;
        int i,j;
        for (i = 0; i <= source.length() - target.length(); i++) {
            for (j = 0; j < target.length(); j++) {
                if (source.charAt(i + j) != target.charAt(j)) break;
            }
            if (j == target.length()) return i;
        }
        return -1;
    }
}

一刷
class Solution {
    /**
     * Returns a index to the first occurrence of target in source,
     * or -1  if target is not part of source.
     * @param source string to be scanned.
     * @param target string containing the sequence of characters to match.
     */
    public int strStr(String source, String target) {
        // write your code here
        if(source == null || target == null) {
            return -1;
        }
        //i is the index to return
        for(int i = 0; i <= source.length() - target.length(); i++) {
            int j = 0;
            for (; j < target.length(); j++) {
                if(source.charAt(i + j) != target.charAt(j)) {
                    break;
                }
            }
         
            if(j == target.length()) {
                return i;
            }
        }
        return -1;
    }
}

No comments:

Post a Comment