滑动窗口+Hash吧, O(n) 的复杂度,比较时只需要对比两个串hash之后的值。 public static int find(String father, String child){ int len1 = father.length(); int len2 = child.length(); if(len1<len2 || len1<=0){ return -1; } // 初始化2的N次方 int[] N2 = new int[26]; for(int i=0; i<26; i++){ N2[i] = (int) Math.pow(2,i); } // 计算child的值 int childValue = 0; for(int i=0; i<len2; i++){ childValue += N2[child.charAt(i)-'a']; } // System.out.println("childValue---"+ childValue); int fatherValue = 0; for(int i=0; i<len1; i++){ if(i>=len2){ fatherValue -= N2[father.charAt(i-len2)-'a']; } fatherValue += N2[father.charAt(i)-'a']; if(fatherValue == childValue){ return i-len2+1; } } return -1; }
点赞 评论

相关推荐

2025-12-28 22:19
门头沟学院 Java
不敢追165女神:简历写得毫无特点,你说你要是大二或者大三找寒假实习到暑期实习这段时间,你的简历还能约到面试。但是你是研究生哥,面试官不会因为你是研究生而降低要求,反而会觉得你是研究生才学了这么一点?为什么我不找个同阶段的本科生?
简历中的项目经历要怎么写
点赞 评论 收藏
分享
01-07 11:46
Java
如图:也是让我遇到逆天公司了,实习生是按天给工资,不忙直接强制休假了
baskly:公司为北京超图软件股份有限公司武汉分公司,明年公司应该会招新实习生,刷到的小伙伴快跑
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务