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;
}
标签:hash,LeetCode438,temp,int,gethash,哈希,字符串,plen 来源: https://blog.csdn.net/qq_43434328/article/details/114852599