题目:对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1

分析:命名有意义;正常的for循环即可;一定有非空检查;数组不要越界

1)常规算法:

2,not ac :循环条件不对,j在循环内定义,判断i+ j,返回值cuo ,

robinhap not ac :target长度没判断,31 ^,长度选错,循环变量错,targethash算错,targetHash = targetHash * 31 % BASE + target.charAt(i) % BASE;没double check

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;
        }
        for (int i = 0; i < source.length() - target.length() + 1; i++) {
            int j = 0;
            for (j = 0; j < target.length(); j++) {
                if (source.charAt(i + j) != target.charAt(j)) {
                    break;
                }
            }
            // loop over
            if (j == target.length()) {
                return i;
            }
        }
        return -1;
    }
}
View Code

 

2)Rabin-Karp算法:利用hash表的原理,把字符串转为数字。最后通过比较hash值是否相同

思路:
base
1、异常检测
2、target长度,为空检测
3、计算31^m(power):边乘边模
4、targethash: 边乘边加边模
5、hashcode :边乘边加边模
i < mi-1:继续
i>m-1:abcd - a ,负数检测
hashcode == targethash
double check

not ac : abcd -a ,hashCode = hashCode - source.charAt(i - m) * power % BASE;忘记模;判断hashcode 是否小于0,写在外面了

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 BASE = 1000000;
    public int strStr(String source, String target) {
        // write your code here
        //非空检查
        if (source == null || target == null) {
            return -1;
        }
        int m = target.length();
        //target长度检查
        if (m <= 0) {
            return 0;
        }
        //31^m
        int pow = 1;
        for (int i = 0; i < m; i++) {
            pow = pow * 31 % BASE;
        }
        //targetcode
        int targetCode = 0;
        for (int i = 0; i < m; i++) {
            targetCode = (targetCode * 31 + target.charAt(i)) % BASE;
        }
        //hashCode
        int hashCode = 0;
        for (int i = 0; i < source.length(); i++) {
            hashCode = (hashCode * 31 + source.charAt(i)) % BASE;
            if (i < m - 1) {
                continue;
            }
            //    i
            // abcd - a
            if (i >= m) {
                hashCode = hashCode - (pow * source.charAt(i - m)) % BASE;
            }
            // hashcode < 0 ?
            if (hashCode < 0) {
                hashCode = hashCode + BASE;
            }
            //hash值是否相等
            if (hashCode == targetCode) {
                //double check
                if (source.substring(i - m + 1, i + 1).equals(target)) {
                    return i - m + 1;
                }
            }
        }
        return -1;
    }
}
View Code

 

posted on 2017-03-06 19:23  Scarlett meng  阅读(247)  评论(0)    收藏  举报