当前位置:网站首页>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;
}边栏推荐
- Shell脚本编程
- Perform general operations on iptables
- C language must memorize code Encyclopedia
- Penetration test (7) -- vulnerability scanning tool Nessus
- 【练习4-1】Cake Distribution(分配蛋糕)
- B - 代码派对(女生赛)
- Find 3-friendly Integers
- [exercise-5] (UVA 839) not so mobile (balance)
- Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
- Data storage in memory & loading into memory to make the program run
猜你喜欢

PySide6 信号、槽

VS2019初步使用

Pyside6 signal, slot

Record of force deduction and question brushing
![[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class](/img/3b/dc43564a36f82e73826b08f39c935e.png)
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class

洛谷P1102 A-B数对(二分,map,双指针)

渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集

Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
![MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’](/img/e6/f4a696179282fe1f4193410c5a493a.png)
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’

X-Forwarded-For详解、如何获取到客户端IP
随机推荐
frida hook so层、protobuf 数据解析
B - 代码派对(女生赛)
socket通讯
Penetration test (7) -- vulnerability scanning tool Nessus
Information security - Analysis of security orchestration automation and response (soar) technology
SSM框架常用配置文件
Truck History
PySide6 信号、槽
Opencv learning log 19 skin grinding
Nodejs+vue online fresh flower shop sales information system express+mysql
TCP's three handshakes and four waves
渗透测试 ( 4 ) --- Meterpreter 命令详解
洛谷P1102 A-B数对(二分,map,双指针)
区间和------离散化
快速转 TypeScript 指南
Nodejs+vue网上鲜花店销售信息系统express+mysql
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
E. Breaking the Wall
【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)