当前位置:网站首页>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;
}
边栏推荐
- Vs2019 initial use
- 【高老师UML软件建模基础】20级云班课习题答案合集
- 1010 things that college students majoring in it must do before graduation
- Research Report on market supply and demand and strategy of China's earth drilling industry
- 【练习-10】 Unread Messages(未读消息)
- F - birthday cake (Shandong race)
- Opencv learning log 30 -- histogram equalization
- Optimization method of path problem before dynamic planning
- C language learning notes
- TCP's three handshakes and four waves
猜你喜欢
STM32 how to use stlink download program: light LED running light (Library version)
D - Function(HDU - 6546)女生赛
X-Forwarded-For详解、如何获取到客户端IP
Data storage in memory & loading into memory to make the program run
frida hook so层、protobuf 数据解析
Basic Q & A of introductory C language
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
C language must memorize code Encyclopedia
滲透測試 ( 1 ) --- 必備 工具、導航
随机推荐
【练习4-1】Cake Distribution(分配蛋糕)
C basic grammar
E. Breaking the Wall
信息安全-威胁检测引擎-常见规则引擎底座性能比较
Ball Dropping
Understand what is a programming language in a popular way
If you want to apply for a programmer, your resume should be written like this [essence summary]
C language is the watershed between low-level and high-level
Information security - threat detection engine - common rule engine base performance comparison
MySQL grants the user the operation permission of the specified content
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
【练习-6】(Uva 725)Division(除法)== 暴力
Perform general operations on iptables
渗透测试 ( 4 ) --- Meterpreter 命令详解
New to redis
F - Birthday Cake(山东省赛)
E. Breaking the Wall
Borg maze (bfs+ minimum spanning tree) (problem solving report)
Borg Maze (BFS+最小生成树)(解题报告)
China potato slicer market trend report, technical dynamic innovation and market forecast