【算法从零到千】【32-36】模拟算法

发布时间:2026/7/17 15:49:54
【算法从零到千】【32-36】模拟算法 1. 替换所有的问号1576. 替换所有的问号https://leetcode.cn/problems/replace-all-s-to-avoid-consecutive-repeating-characters/给你一个仅包含小写英文字母和?字符的字符串s请你将所有的?转换为若干小写字母使最终的字符串不包含任何 连续重复 的字符。注意你 不能 修改非?字符。题目测试用例保证 除?字符 之外不存在连续重复的字符。在完成所有转换可能无需转换后返回最终的字符串。如果有多个解决方案请返回其中任何一个。可以证明在给定的约束条件下答案总是存在的。class Solution { public: string modifyString(string s) { int ns.size(); for(int i0;in;i) { if(s[i]?) { for(char sta;stz;st) { if((i0||st!s[i-1])(in-1||st!s[i1])) s[i]st;break; } } } return s; } };2. 提莫攻击495. 提莫攻击https://leetcode.cn/problems/teemo-attacking/在《英雄联盟》的世界中有一个叫 “提莫” 的英雄。他的攻击可以让敌方英雄艾希编者注寒冰射手进入中毒状态。当提莫攻击艾希艾希的中毒状态正好持续duration秒。正式地讲提莫在t发起攻击意味着艾希在时间区间[t, t duration - 1]含t和t duration - 1处于中毒状态。如果提莫在中毒影响结束 前 再次攻击中毒状态计时器将会 重置 在新的攻击之后中毒影响将会在duration秒后结束。给你一个 非递减 的整数数组timeSeries其中timeSeries[i]表示提莫在timeSeries[i]秒时对艾希发起攻击以及一个表示中毒持续时间的整数duration。返回艾希处于中毒状态的 总 秒数。class Solution { public: int findPoisonedDuration(vectorint timeSeries, int duration) { int sum0,x0; for(int i1;itimeSeries.size();i) { xtimeSeries[i]-timeSeries[i-1]; if(xduration) sumduration; else sumtimeSeries[i]-timeSeries[i-1]; } return sumduration; } };3. Z字形变换6. Z 字形变换https://leetcode.cn/problems/zigzag-conversion/将一个给定字符串s根据给定的行数numRows以从上往下、从左到右进行 Z 字形排列。比如输入字符串为PAYPALISHIRING行数为3时排列如下P A H N A P L S I I G Y I R之后你的输出需要从左往右逐行读取产生出一个新的字符串比如PAHNAPLSIIGYIR。请你实现这个将字符串进行指定行数变换的函数string convert(string s, int numRows);方法一模拟二维数组class Solution { public: string convert(string s, int numRows) { if (numRows 1) return s; vectorstring rows(numRows); int i 0, step 1; for (char c : s) { rows[i] c; if (i 0) step 1; if (i numRows - 1) step -1; i step; } string res; for (string r : rows) res r; return res; } };方法二数学规律string convert(string s, int numRows) { if (numRows 1) return s; int step numRows * 2 - 2; // 间距 int index 0;// 记录s的下标 int len s.length(); int add 0; // 真实的间距 string ret; for (int i 0; i numRows; i) // i表示行号 { index i; add i * 2; while (index len)//超出字符串长度计算下一层 { ret s[index]; // 当前行的第一个字母 add step - add;// 第一次间距是step -2*i第二次是2*i, index (i 0 || i numRows-1) ? step : add; // 0行和最后一行使用step间距其余使用add间距 } } return ret; }4. 外观数列38. 外观数列https://leetcode.cn/problems/count-and-say/「外观数列」是一个数位字符串序列由递归公式定义countAndSay(1) 1countAndSay(n)是countAndSay(n-1)的行程长度编码。行程长度编码RLE是一种字符串压缩方法其工作原理是通过将每个最大连续相同字符组替换为该组的长度后加上该字符本身。例如要压缩字符串3322251我们将33用23替换将222用32替换将5用15替换并将1用11替换。因此压缩后字符串变为23321511。给定一个整数n返回 外观数列 的第n个元素。输入n 4输出1211解释countAndSay(1) 1countAndSay(2) 1 的行程长度编码 11countAndSay(3) 11 的行程长度编码 21countAndSay(4) 21 的行程长度编码 1211class Solution { public: string countAndSay(int n) { int time1,count1; string s1,s2; while(timen) { for(int i0;is.size();i) { if(i1s.size()s[i]s[i1]) count; else if(i1s.size()||s[i]!s[i1]) { s2.push_back(0count);//⭐ s2.push_back(s[i]); count1; } } ss2; s2.clear(); time; } return s; } };⭐5. 数青蛙1419. 数青蛙https://leetcode.cn/problems/minimum-number-of-frogs-croaking/给你一个字符串croakOfFrogs它表示不同青蛙发出的蛙鸣声字符串croak的组合。由于同一时间可以有多只青蛙呱呱作响所以croakOfFrogs中会混合多个“croak”。请你返回模拟字符串中所有蛙鸣所需不同青蛙的最少数目。要想发出蛙鸣 croak青蛙必须 依序 输出‘c’, ’r’, ’o’, ’a’, ’k’这 5 个字母。如果没有输出全部五个字母那么它就不会发出声音。如果字符串croakOfFrogs不是由若干有效的 croak 字符混合而成请返回-1写法一class Solution { public: int minNumberOfFrogs(string croakOfFrogs) { // 预处理每个字母在 croak 中的上一个字母 char PREVIOUS[s]; // s 保证 croak 中的任意字符都不会越界 const string croak croakc; for (int i 1; i croak.length(); i) { PREVIOUS[croak[i]] croak[i - 1]; } int cnt[s]{}; for (char ch: croakOfFrogs) { char pre PREVIOUS[ch]; // pre 为 ch 在 croak 中的上一个字母 if (cnt[pre]) { // 如果有青蛙发出了 pre 的声音 cnt[pre]--; // 复用一只 } else if (ch ! c) { // 否则青蛙必须从 c 开始蛙鸣 return -1; // 不符合要求 } cnt[ch]; // 发出了 ch 的声音 } if (cnt[c] || cnt[r] || cnt[o] || cnt[a]) { return -1; // 有发出其它声音的青蛙不符合要求 } return cnt[k]; // 最后青蛙们都发出了 k 的声音 } };写法二class Solution { public: int minNumberOfFrogs(string croakOfFrogs) { int count[4]{},res0,sum0,k0; for(char x:croakOfFrogs){ switch(x){ case c:count[0];sum;break; case r:k0;count[0]--;count[1];break; case o:k1;count[1]--;count[2];break; case a:k2;count[2]--;count[3];break; default:k3;sum--;count[3]--;break; } if(count[k]0)return -1; if(sumres)ressum; } return sum?-1:res; } };写法三class Solution { public: int minNumberOfFrogs(string croakOfFrogs) { int n croakOfFrogs.size(), ans 0; int c 0, r 0, o 0, a 0, k 0; for(int i 0; i n; i) { if(croakOfFrogs[i] c) c; if(croakOfFrogs[i] r) r; if(croakOfFrogs[i] o) o; if(croakOfFrogs[i] a) a; if(croakOfFrogs[i] k) k; if(!(c r r o o a a k)) return -1; ans max(ans, c - k); } return c r r o o a a k? ans: -1; } };