当前位置:网站首页>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;
}
边栏推荐
- JS调用摄像头
- Determine the Photo Position
- 信息安全-威胁检测引擎-常见规则引擎底座性能比较
- Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
- Interesting drink
- HDU-6025-Coprime Sequence(女生赛)
- MySQL grants the user the operation permission of the specified content
- [exercise-5] (UVA 839) not so mobile (balance)
- Luogu P1102 A-B number pair (dichotomy, map, double pointer)
- Determine the Photo Position
猜你喜欢
Borg maze (bfs+ minimum spanning tree) (problem solving report)
Determine the Photo Position
Basic Q & A of introductory C language
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
7-1 懂的都懂 (20 分)
B - 代码派对(女生赛)
Borg Maze (BFS+最小生成树)(解题报告)
Penetration test (1) -- necessary tools, navigation
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
Pyside6 signal, slot
随机推荐
Matlab comprehensive exercise: application in signal and system
Borg maze (bfs+ minimum spanning tree) (problem solving report)
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
JS调用摄像头
D - Function(HDU - 6546)女生赛
F - Birthday Cake(山东省赛)
[exercise-8] (UVA 246) 10-20-30== simulation
差分(一维,二维,三维) 蓝桥杯三体攻击
Nodejs+vue网上鲜花店销售信息系统express+mysql
快速转 TypeScript 指南
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
【高老师UML软件建模基础】20级云班课习题答案合集
b站 实时弹幕和历史弹幕 Protobuf 格式解析
The concept of C language array
[exercise-3] (UVA 442) matrix chain multiplication
Interval sum ----- discretization
对iptables进行常规操作
Opencv learning log 32 edge extraction
Opencv learning log 30 -- histogram equalization
HDU - 6024 building shops (girls' competition)