学习目标:
- 28. 实现 strStr()
- 459.重复的子字符串
- 认证授权面试题总结
学习内容:
28. 实现 strStr()
题目链接 &&文章讲解
给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。
class Solution {
public int strStr(String haystack, String needle) {
int[] next = new int[needle.length()];
getNext(next,needle);
int j = 0;
for(int i = 0 ; i <haystack.length(); i++){
while (j > 0 && needle.charAt(j) != haystack.charAt(i))
j = next[j - 1];
if (needle.charAt(j) == haystack.charAt(i))
j++;
if (j == needle.length())
return i - needle.length() + 1;
}
return -1;
}
public void getNext(int[] next , String s){
//i:后缀末尾位置
//j:前缀末尾位置 && 最长相等前后缀长度
//1.初始化
int j = 0;
next[0] = 0;
for(int i = 1; i < s.length(); i++){
//前后缀不相等:回退
while(j > 0 && s.charAt(i) != s.charAt(j)){
j = next[j-1];
}
//前后缀相等:j++
if(s.charAt(i) == s.charAt(j)) j++;
//更新next数组的值
next[i] = j;
}
}
}
459.重复的子字符串
题目链接&&文章讲解
给定一个非空的字符串 s ,检查是否可以通过由它的一个子串重复多次构成。
class Solution {
public boolean repeatedSubstringPattern(String s) {
//重复子串:最长相等前缀和最长相等后缀不包含的子串
int[] next = new int[s.length()];
getNext(next, s);
int len = s.length();
if (next[len - 1] != 0 && len % (len - next[len - 1]) == 0) {
return true;
}
return false;
}
public void getNext(int[] next , String s){
//i:后缀末尾位置
//j:前缀末尾位置 && 最长相等前后缀长度
//1.初始化
int j = 0;
next[0] = 0;
for(int i = 1; i < s.length(); i++){
//前后缀不相等:回退
while(j > 0 && s.charAt(i) != s.charAt(j)){
j = next[j-1];
}
//前后缀相等:j++
if(s.charAt(i) == s.charAt(j)) j++;
//更新next数组的值
next[i] = j;
}
}
}
认证授权面试题总结
- 认证和授权的概念
- JWT概念以及优缺点
- SSO单点登录