一、无重复字符的最长子串(子串需要连续)
题目描述:给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
例子:
输入: s = “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
思路:
利用哈希表,遍历字符串s,若字母之前未出现,则将其下标保存起来,若该字母在前面出现时,获取其下标并计算长度(此时为不重复的子串长度),接着更新起始的子串下标。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n=s.length();
int ans=0;
int pre=0;//记录答案子串的起始下标
unordered_map<char,int>mp;//存放字母以及下标
int i=0;
for(;i<n;i++){
if(mp.find(s[i])==mp.end()){//未出现,则保存
mp.emplace(s[i],i);
}
else{
int k=mp[s[i]];//得到其下标
mp[s[i]]=i;
if(k>=pre&&ans<i-pre){
ans=i-pre;
}
pre=max(pre,k+1); //更新起始下标
}
}
if(ans<i-pre){
ans=i-pre;
}
return ans;
}
};
二、不同字符的最小子序列(字典序)
题目描述:返回 s 字典序最小的子序列,该子序列包含 s 的所有不同字符,且只包含一次。
例子:
输入:s = “cbacdcbc”
输出:“acdb”
class Solution {
public:
string smallestSubsequence(string s) {
int n=s.length();
vector<int>num(26,0);//记录s中各个元素的个数
vector<int>vis(26,0);//判断某一字符是否已经进入
string temp;
for(int i=0;i<n;i++){
num[s[i]-'a']++;
}
for(int i=0;i<n;i++){
if(!vis[s[i]-'a']){
while(!temp.empty()&&s[i]<temp.back()){
if(num[temp.back()-'a']>0){
vis[temp.back()-'a']=0;
temp.pop_back();
}
else{
break;
}
}
temp.push_back(s[i]);
vis[s[i]-'a']=1;//已存放在栈里
}
num[s[i]-'a']--;
}
return temp;
}
};
三、最长公共子序列(不需要连续)(典型二维dp)
题目描述:
给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。
一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
例子:
输入:text1 = “abcde”, text2 = “ace”
输出:3
解释:最长公共子序列是 “ace” ,它的长度为 3 。
思路:
当text1[i]==text2[j]时,dp[i][j]=dp[i-1][j-1]
当text1[i]!=text2[j]时,dp[i][j]=max(d[i-1][j],dp[i][j-1])
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
int n1=text1.length();
int n2=text2.length();
int dp[1002][1002];
for(int i=0;i<=n1;i++){//初始化时要留意
dp[i][0]=0;
}
for(int i=0;i<=n2;i++){
dp[0][i]=0;
}
for(int i=1;i<=n1;i++){
for(int j=1;j<=n2;j++){
if(text1[i-1]==text2[j-1]){
dp[i][j]=dp[i-1][j-1]+1;
}
else{
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
}
}
}
return dp[n1][n2];
}
};
四、KMP算法
class Solution {
public:
void CreateNext(vector<int>&nextval,string needle){
int k=-1;
int j=0;
nextval[0]=-1;
while(j<needle.length()-1){
if(k==-1||needle[j]==needle[k]){
j++;
k++;
//当a位与其next[a]的字符相等时,让next[a]=next[next[a]]
if(needle[j]!=needle[k]){
nextval[j]=k;
}
else{
nextval[j]=nextval[k];
}
}
else{
k=nextval[k];
}
}
}
int strStr(string haystack, string needle) {
int n1=haystack.length();
int n2=needle.length();
if(n2==0){
return 0;
}
vector<int>nextval(n2,0);
CreateNext(nextval,needle);
int i=0,j=0;
while(i<n1&&j<n2){
if(j==-1||haystack[i]==needle[j]){
i++;
j++;
}
else{
j=nextval[j];
}
}
if(j>=n2){
return i-n2;
}
else{
return -1;
}
}
};