LeetCode438找到字符串中所有字母异位词(字符串哈希)

题目链接
LeetCode438找到字符串中所有字母异位词(字符串哈希)
字符串哈希 对哈希的方法还是有点理解的不好

    public List<Integer> findAnagrams(String s, String p) {
        int phash = gethash(p);
        int plen = p.length();
        int slen = s.length();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < slen - plen + 1; i++) {
            int ss = gethash(s.substring(i,i+plen));
            if (phash == ss)
                list.add(i);
        }
        return list;
    }
    private static int gethash(String str){
        int hash = 0;
        for (int i = 0; i < str.length(); i++) {
            int temp = ((str.charAt(i)-'a'+132)*100+36)/27;
            hash += temp*temp*temp;
        }
        return hash;
    }
上一篇:CSAPP:Binary Bomb


下一篇:Educational Codeforces round 78 A、B