当前位置:网站首页>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;
}边栏推荐
- Research Report on shell heater industry - market status analysis and development prospect forecast
- [exercise-2] (UVA 712) s-trees
- MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
- Frida hook so layer, protobuf data analysis
- [exercise -10] unread messages
- Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
- 【练习-9】Zombie’s Treasure Chest
- Basic Q & A of introductory C language
- Perform general operations on iptables
- Research Report of cylindrical grinder industry - market status analysis and development prospect forecast
猜你喜欢

b站 實時彈幕和曆史彈幕 Protobuf 格式解析

Information security - threat detection engine - common rule engine base performance comparison

渗透测试 ( 3 ) --- Metasploit Framework ( MSF )

信息安全-安全编排自动化与响应 (SOAR) 技术解析

Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B

B - 代码派对(女生赛)

Determine the Photo Position

1010 things that college students majoring in it must do before graduation
![[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
快速转 TypeScript 指南
随机推荐
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
Perform general operations on iptables
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
E. Breaking the Wall
Information security - threat detection - detailed design of NAT log access threat detection platform
CEP used by Flink
PySide6 信号、槽
[exercise-3] (UVA 442) matrix chain multiplication
C language must memorize code Encyclopedia
[exercise-2] (UVA 712) s-trees
最全编程语言在线 API 文档
[exercise 4-1] cake distribution
Penetration test (3) -- Metasploit framework (MSF)
栈的经典应用—括号匹配问题
[exercise-6] (UVA 725) division = = violence
Gartner: five suggestions on best practices for zero trust network access
China's peripheral catheter market trend report, technological innovation and market forecast
C language learning notes
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class