当前位置:网站首页>Leetcode2027. 转换字符串的最少操作次数(可以,一次过)
Leetcode2027. 转换字符串的最少操作次数(可以,一次过)
2022-06-11 19:39:00 【我不是萧海哇~~~~】

给你一个字符串 s ,由 n 个字符组成,每个字符不是 ‘X’ 就是 ‘O’ 。
一次 操作 定义为从 s 中选出 三个连续字符 并将选中的每个字符都转换为 ‘O’ 。注意,如果字符已经是 ‘O’ ,只需要保持 不变 。
返回将 s 中所有字符均转换为 ‘O’ 需要执行的 最少 操作次数。
示例 1:
输入:s = "XXX"
输出:1
解释:XXX -> OOO
一次操作,选中全部 3 个字符,并将它们转换为 'O' 。
示例 2:
输入:s = "XXOX"
输出:2
解释:XXOX -> OOOX -> OOOO
第一次操作,选择前 3 个字符,并将这些字符转换为 'O' 。
然后,选中后 3 个
字符,并执行转换。最终得到的字符串全由字符 ‘O’ 组成。
示例 3:
输入:s = "OOOO"
输出:0
解释:s 中不存在需要转换的 'X' 。
提示:
- 3 <= s.length <= 1000
- s[i] 为 ‘X’ 或 ‘O’
Code:
class Solution {
public:
int minimumMoves(string s) {
int res=0;
for(int i=0;i<s.length();i++)
{
if(s[i]=='X')
{
int cnt=0;
if((i+1)<s.length())
{
cnt++;
s[i]='O';
}
if((i+2)<s.length())
{
cnt++;
s[i]='O';
}
i+=cnt;
res++;
}
}
return res;
}
};
边栏推荐
- 556. next larger element iii- (31. next permutation) - two iterations
- 图床:PicGo+腾讯云+typora
- Merge multiple binary search trees
- Hospital intelligent infusion management system source code hospital source code
- 【Bug解决】UnpicklingError: A load persistent id instruction was encountered, but no persistent_load.
- Course design for construction organization, School of distance and continuing education, Dalian University of technology [standard answer]
- "Case sharing" based on am57x+ artix-7 FPGA development board - detailed explanation of Pru Development Manual
- Detailed explanation of iSCSI (IV) -- actual configuration of iSCSI server
- 金字塔测试原理:写好单元测试的8个小技巧,一文总结
- 【 aide 】 comment puis - je faire en sorte que les messages sélectionnés ci - dessous puissent être affichés après l'ouverture de l'article Wechat public number dans un navigateur externe?
猜你喜欢

Multimodal learning toolkit paddlemm based on propeller

Review of software testing technology
【求助】請問如何讓微信公眾號文章在外部瀏覽器中打開後還能顯示下方的精選留言?

Calculate the number of 926 in the string to the power of 9260 of 926
CMU 15-445 database course lesson 5 text version - buffer pool

Highcharts sets the histogram width, gradient, fillet, and data above the column

何恺明团队的“视频版本MAE”,高效视频预训练!Mask Ratio高达90%时效果也很好!...

2022 the latest software testing classic summarized by major manufacturers. After reading it, I'm not afraid I won't get an offer

Specific methods for porting WinCC flexible 2008 project to botu WinCC

Postman configuration Chinese
随机推荐
MOS transistor 24n50 parameters of asemi, 24n50 package, 24n50 size
556. next larger element iii- (31. next permutation) - two iterations
SLAM APP
干货丨MapReduce的工作流程是怎样的?
collect. stream(). Use of the collect() method
Tensorflow---TFRecord文件的创建与读取
2022 the latest software testing classic summarized by major manufacturers. After reading it, I'm not afraid I won't get an offer
5g communication test manual based on Ti am5728 + artix-7 FPGA development board (dsp+arm)
On high availability architecture
LDPC 7 - simple example of decoding
计算926的9260次方里的字符串里有多少个926
01. Telecommunications_ Field business experience
This article introduces you to the infrastructure of sofaregistry
干货!基于序列超图神经网络的信息扩散预测
Proficient in xmake
AHB_Bus_Matrix_3x3 设计
AHB2APB_bridge 设计
[bug resolution] unpickingerror: a load persistent ID instruction was encoded, but no persistent_ load.
SISO decoder for repetition (supplementary Chapter 4)
Introduction to go language (V) -- branch statement