当前位置:网站首页>Sword finger offer special assault edition day 6
Sword finger offer special assault edition day 6
2022-07-22 14:39:00 【hys__ handsome】
The finger of the sword Offer II 017. The shortest string containing all characters
hard topic ( Time beats 100%)
Double pointer ( j j j Used to extend string , i i i Used to shorten the string ), Time complexity O ( n ) O(n) O(n)
class Solution {
public:
string minWindow(string s, string t) {
int n = s.size(), m = t.size();
int len = 0x3f3f3f3f;
string res;
int tst[200] = {
0}; // Mark the number of characters separately
int tcnt = 0; // Different number of characters
for(int i = 0; i < m; i++) {
if(!tst[t[i]]) tcnt++;
tst[t[i]]++;
}
int sst[200] = {
0}; // Mark the number of characters separately
bool st[200] = {
0}; // Mark s The substring has been greater than or equal to t The characters in
int i = 0, j = 0, scnt = 0; //scnt by st The array is true The number of
while(j < n) {
if(tst[s[j]] != 0) {
stst[s[j]]++;
if(!st[s[j]] && sst[s[j]] >= tst[s[j]]) {
scnt++;
st[s[j]] = true;
}
}
if(scnt == tcnt) {
//s The substring contains t All the characters in , Start moving i To reduce the length of the substring
while(!tst[s[i]] || stst[s[i]] - 1 >= tst[s[i]]) {
stst[s[i++]]--;
}
if(len > j-i+1) {
len = j-i+1;
res = s.substr(i,len);
}
}
j++;
}
return res;
}
};
The finger of the sword Offer II 018. Valid palindromes
Simple palindrome string
class Solution {
public:
bool isPalindrome(string s) {
int n = s.size();
string tmp;
for(char c : s) {
if(c >= '0' && c <= '9') tmp += c;
else if(c >= 'a' && c <= 'z') tmp += c;
else if(c >= 'A' && c <= 'Z') tmp += (char)(c+32);
}
n = tmp.size();
for(int i = 0; i < n/2; i++)
if(tmp[i] != tmp[n-i-1])
return false;
return true;
}
};
The finger of the sword Offer II 019. Delete at most one character to get a palindrome
It is more complicated than ordinary palindrome strings , Good writing abstracts and separates redundant modules
class Solution {
public:
bool check(string &s, int l,int r) {
while(l < r) {
if(s[l] != s[r]) return false;
else l++,r--;
}
return true;
}
bool validPalindrome(string s) {
int i = 0, j = s.size()-1;
int idx1 = -1, idx2 = -1;
while(i < j) {
if(s[i] == s[j]) {
i++,j--;
} else {
return check(s,i+1,j) || check(s,i,j-1);
}
}
return true;
}
};
The finger of the sword Offer II 020. Number of palindrome substrings
character string +DP
O ( n 2 ) O(n^2) O(n2) practice
class Solution {
public:
int countSubstrings(string s) {
int n = s.size();
int cnt = 0;
bool st[1010][1010] = {
0};
for(int i = 0; i < n; i++) {
st[i][i] = true; cnt++;
for(int j = 0; j < i; j++){
if(s[j] == s[i]) {
if(i-j+1 > 2 && st[j+1][i-1] != true) continue;
cnt++, st[j][i] = true;
}
}
}
return cnt;
}
};
O ( n ) O(n) O(n)dp practice
Reference resources :https://leetcode.cn/problems/a7VOhD/solution/hui-wen-zi-zi-fu-chuan-de-ge-shu-by-leet-ejfv/
class Solution {
public:
int countSubstrings(string s) {
int n = s.size();
string t = "$#";
for (const char &c: s) {
t += c;
t += '#';
}
n = t.size();
t += '!';
auto f = vector <int> (n);
int iMax = 0, rMax = 0, ans = 0;
for (int i = 1; i < n; ++i) {
// initialization f[i]
f[i] = (i <= rMax) ? min(rMax - i + 1, f[2 * iMax - i]) : 1;
// Center expansion
while (t[i + f[i]] == t[i - f[i]]) ++f[i];
// Dynamic maintenance iMax and rMax
if (i + f[i] - 1 > rMax) {
iMax = i;
rMax = i + f[i] - 1;
}
// Statistical answer , The current contribution is (f[i] - 1) / 2 Round up
ans += (f[i] / 2);
}
return ans;
}
};
边栏推荐
- SOC第一个工程
- DCM10- 安全访问 ($27)的功能和配置【基于DaVinci Configurator Classic】
- 腾讯测试岗,被面试官虐哭...直到学长给了我这些知识点
- Three barriers in the workplace: annual salary of 300000, 500000 and 1million
- Design of consumption system of unmanned Supermarket Based on stm32
- Tas (file d'attente prioritaire)
- BERTopic
- 查找问题:顺序查找与二分法查找
- 2022 crane driver (bridge crane only) examination question bank and answers
- 代码规范的一些经验
猜你喜欢
When wechat is opened, it supports message notification banners to attract hot discussion; The cloud services of Google and Oracle were offline due to the high temperature weather in the UK; Google re
1. Monitoring concept
解决win10莫名其妙重启问题
[multithreading] is there only one way to implement threads or four ways
丘成桐大学生数学竞赛数学物理
Mysql索引分類及其使用實例
每日刷题记录 (三十)
2. ZABBIX concept
MySql集群之集群配置与验证(四)
【服务器数据恢复】华为某型号服务器raid6数据恢复案例
随机推荐
classes. Jar: another program is using this file, and the process cannot access it.
Single page reference record last sliding position
半月谈 总结中前行
Electron builder successfully packed serialport
基于STM32无人超市消费系统设计
【深度学习】YOLO转VOC VOC转YOLO
音量调节堆栈
【Pingtunnel工具教程】利用ICMP隧道技术进行ICMP封装穿透防火墙
中年危机,关于未来的一些思考
Tencent test post, was abused by the interviewer... Until the senior gave me these knowledge points
[idea] common shortcut keys of idea
About Cordova referencing AAR package
狂神redis笔记08
The difference and connection between cookies and seesion
【idea】idea常用快捷键
时间复杂度与复杂度
electron-builder 打包 serialport 成功
MySql集群之主从复制(一)
Requirement: how to import the MD file containing the public drawing bed pictures into the language bird in the form of graphics and text- 2022.7.17 (resolved)
微波雷达人体感应器,即时存在感知方案,智能家居人体感应交互