当前位置:网站首页>2027. Minimum number of operations to convert strings
2027. Minimum number of operations to convert strings
2022-07-06 16:07:00 【mrbone9】
Address :
Power button https://leetcode-cn.com/problems/minimum-moves-to-convert-string/
subject :
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 Characters , 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' |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/minimum-moves-to-convert-string
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
Flop once yes 3 Elements , If the first element is 'O', It's a waste of opportunity , So looking for elements directly is 'X' The beginning of
Every time 3 One for a group , Till the end , Pay attention to the lower boundary
Method 1 、 find 'X' At the beginning 3 Tuples
int minimumMoves(char * s){
int slen = strlen(s);
int moves = 0;
int i=0;
while(s[i])
{
if(s[i] == 'O')
i++;
else
{
if( (i+3) <= slen )
{
moves++;
i += 3;
}
else
{
moves++;
break;
}
}
}
return moves;
}
边栏推荐
- What is the difficulty of programming?
- 【练习-6】(Uva 725)Division(除法)== 暴力
- MySQL授予用户指定内容的操作权限
- 渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
- Opencv learning log 30 -- histogram equalization
- MySQL grants the user the operation permission of the specified content
- C basic grammar
- Information security - Analysis of security orchestration automation and response (soar) technology
- 栈的经典应用—括号匹配问题
- Shell Scripting
猜你喜欢
Ball Dropping
【高老师软件需求分析】20级云班课习题答案合集
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
B - Code Party (girls' competition)
快速转 TypeScript 指南
Record of force deduction and question brushing
差分(一维,二维,三维) 蓝桥杯三体攻击
Information security - Analysis of security orchestration automation and response (soar) technology
Essai de pénétration (1) - - outils nécessaires, navigation
随机推荐
Ball Dropping
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
China's peripheral catheter market trend report, technological innovation and market forecast
Research Report on market supply and demand and strategy of China's earth drilling industry
[exercise-8] (UVA 246) 10-20-30== simulation
Borg Maze (BFS+最小生成树)(解题报告)
F - birthday cake (Shandong race)
Alice and Bob (2021 Niuke summer multi school training camp 1)
Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
Programmers, what are your skills in code writing?
栈的经典应用—括号匹配问题
Pyside6 signal, slot
b站 实时弹幕和历史弹幕 Protobuf 格式解析
F - Birthday Cake(山东省赛)
Interesting drink
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
CEP used by Flink
Research Report of cylindrical grinder industry - market status analysis and development prospect forecast
D - function (HDU - 6546) girls' competition
7-1 懂的都懂 (20 分)