当前位置:网站首页>Leetcode2027. Minimum number of operations to convert a string (yes, once)
Leetcode2027. Minimum number of operations to convert a string (yes, once)
2022-06-11 19:40:00 【I'm not xiaohaiwa~~~~】

Give you a string s , from n Characters make up , Each character is not ‘X’ Namely ‘O’ .
once operation Defined as from s Selected from Three consecutive characters And convert each selected character to ‘O’ . Be careful , If the character is already ‘O’ , Just keep unchanged .
Return to s All characters in are converted to ‘O’ executable least Operating frequency .
Example 1:
Input :s = "XXX"
Output :1
explain :XXX -> OOO
One operation , Select All 3 Characters , And turn them into 'O' .
Example 2:
Input :s = "XXOX"
Output :2
explain :XXOX -> OOOX -> OOOO
The first operation , Choose the former 3 Characters , And convert these characters to 'O' .
then , After selection 3 individual
character , And perform the conversion . The resulting string consists entirely of characters ‘O’ form .
Example 3:
Input :s = "OOOO"
Output :0
explain :s There is no need to convert 'X' .
Tips :
- 3 <= s.length <= 1000
- s[i] by ‘X’ or ‘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;
}
};
边栏推荐
- 【Bug解决】UnpicklingError: A load persistent id instruction was encountered, but no persistent_load.
- Summary 111111111111111111111
- Understand how to get started with machine learning to quantify transactions?
- Xmake help 2
- YOLOv3 Pytorch代码及原理分析(二):网络结构和 Loss 计算
- CMU 15-445 database course lesson 5 text version - buffer pool
- 【 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?
- 何恺明团队的“视频版本MAE”,高效视频预训练!Mask Ratio高达90%时效果也很好!...
- [C language questions -- 10 simple questions for leetcode]
- How are functional components different from class components
猜你喜欢

Review of software testing technology

RTL仲裁器设计

Hyper parameter optimization of deep neural networks using Bayesian Optimization

Merge multiple binary search trees

"Case sharing" based on am57x+ artix-7 FPGA development board - detailed explanation of Pru Development Manual

AHB2Standard_handshake_bridge 设计

2022 the latest software testing classic summarized by major manufacturers. After reading it, I'm not afraid I won't get an offer
【求助】请问如何让微信公众号文章在外部浏览器中打开后还能显示下方的精选留言?

AHB_ Bus_ Matrix_ 3x3 design

Yolov3 pytoch code and principle analysis (II): network structure and loss calculation
随机推荐
MySQL - Basic select statement
LDPC 7 - simple example of decoding
APB2standard_handshake_bridge 设计
Yolov3 pytoch code and principle analysis (II): network structure and loss calculation
何恺明团队的“视频版本MAE”,高效视频预训练!Mask Ratio高达90%时效果也很好!...
Summary 111111111111111111111
Lecture 30 linear algebra Lecture 2 Matrix
Automated test requirements analysis
SISO decoder for repetition (supplementary Chapter 4)
APB2standard_ handshake_ Bridge design
Building web applications
MySQL federated index and BTREE
RTL仲裁器设计
Operator new and placement new
图像变换 torchvision.transforms 笔记
Proficient in xmake
Merge multiple binary search trees
Find the maximum 3 same digits in the string
Introduction to go language (V) -- branch statement
Expandable type of system