当前位置:网站首页>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;
}
};
边栏推荐
- 计算926的9260次方里的字符串里有多少个926
- On high availability architecture
- Experiment report of basic mechanical experiment (II) of School of distance and continuing education, Dalian University of technology [standard answer]
- postman配置中文
- RTL仲裁器设计
- NR LDPC punched
- Multimodal learning toolkit paddlemm based on propeller
- Calculate the number of 926 in the string to the power of 9260 of 926
- MySQL - Basic select statement
- Review of software testing technology
猜你喜欢

YOLOv3 Pytorch代码及原理分析(二):网络结构和 Loss 计算

Poj1028 web navigation

Understand how to get started with machine learning to quantify transactions?

Yolov3 pytoch code and principle analysis (II): network structure and loss calculation

CMU 15 - 445 cours de base de données Leçon 5 version texte - Pool tampon

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

Pyramid test principle: 8 tips for writing unit tests

3D建模有什么技巧吗?

Neural network and deep learning-2-simple example of machine learning pytorch

构建Web应用程序
随机推荐
图像变换 torchvision.transforms 笔记
CMU 15-445 database course lesson 5 text version - buffer pool
基于飞桨搭建的多模态学习工具包PaddleMM
Pyramid test principle: 8 tips for writing unit tests
MySQL - Basic select statement
Pyqt5 tips - button vertical display method, QT designer sets button vertical display.
Qubicle notes: self set shortcut keys (attached with Lao Wang's self set shortcut key file)
Go语言入门(六)——循环语句
Understand how to get started with machine learning to quantify transactions?
2022各大厂最新总结的软件测试宝典,看完不怕拿不到offer
谷歌提出超强预训练模型CoCa,在ImageNet上微调Top-1准确率达91%!在多个下游任务上SOTA!...
cocan yocto buildroot
What started mongodb? What are the application scenarios?
PIL pilot image processing [1] - installation and creation
SISO decoder for SPC (supplementary Chapter 1)
Gmail: how do I recall an outgoing message?
Chrome tips - browser web page setting coding, solving the problem of web page garbled code, obtaining the latest version of charset plug-in, UTF-8 coding setting
无监督图像分类《SCAN:Learning to Classify Images without》代码分析笔记(1):simclr
Operator new and placement new
MySQL——事务