当前位置:网站首页>Leetcode 1963. Minimum number of swaps to balance strings (learning)
Leetcode 1963. Minimum number of swaps to balance strings (learning)
2022-06-11 14:04:00 【I'm not xiaohaiwa~~~~】
Give you a string s , Subscript from 0 Start , And the length is even n . character string just from n / 2 An open bracket ‘[’ and n / 2 Closed parentheses ‘]’ form .
Only a string that satisfies all of the following conditions can be called Balanced string :
- The string is an empty string , perhaps
- Strings can be written as AB , among A and B All are Balanced string , perhaps
- The string can be written as [C] , among C It's a Balanced string .
You can exchange arbitrarily The parentheses corresponding to the two subscripts arbitrarily frequency .
Return to make s become Balanced string The required Minimum Number of exchanges .
Example 1:
Input :s = "][]["
Output :1
explain : Swap subscripts 0 And subscripts 3 Corresponding parentheses , You can make a string into a balanced string .
The final string becomes "[[]]" .
Example 2:
Input :s = "]]][[["
Output :2
explain : Do the following to make the string balanced :
- Swap subscripts 0 And subscripts 4 Corresponding parentheses ,s = "[]][][" .
- Swap subscripts 1 And subscripts 5 Corresponding parentheses ,s = "[[][]]" .
The final string becomes "[[][]]" .
Example 3:
Input :s = "[]"
Output :0
explain : This string is already a balanced string .
Tips :
- n == s.length
- 2 <= n <= 106
- n For the even
- s[i] by ’[’ or ‘]’
- Open bracket ‘[’ The number is n / 2 , close-quote ‘]’ The number of n / 2
Netizen code
Code:
class Solution {
public:
int minSwaps(string s) {
stack<char> stk;
for (char c : s) {
if (c == ']' && !stk.empty() && stk.top() == '[') {
stk.pop();
}
else {
stk.push(c);
}
}
return (stk.size() / 2 + 1) / 2;;
}
};
边栏推荐
- Kubernetes binary installation (v1.20.16) (V) verifying master deployment
- Solve the circular dependency bug. Relying upon circular references is discouraged and they are prohibited by default.
- Implementation of VGA protocol based on FPGA
- RS485(Modbus RTU)工业RFID读写器CK-FR03-A01与PLC三菱FX5U的通讯操作说明
- 2022工具钳工(中级)操作证考试题库及答案
- mysql高级语句
- Nomad application scheduling scheme 08 of hashicopy (detection job)
- Unsealing easy QF PDA helps enterprises improve ERP management
- Sqlmap detection SQL lab range
- 复选框 全选or取消全选
猜你喜欢

The application of machine learning in database cardinality estimation

Operating instructions for communication between RS485 (Modbus RTU) industrial RFID reader ck-fr03-a01 and PLC Mitsubishi fx5u

Leetcode 1968. 构造元素不等于两相邻元素平均值的数组(可以,终于解决)

How to quickly compress the size of video?

代码对比工具,我就用这6个

Two small things, feel the gap with the great God

Can't understand kotlin source code? Starting with the contracts function~

Vi LXD deployment of lab server for development records

Why does each running Department display that the database already exists, delete the database, and then succeed? Each running department must delete the database, and re run the whole successfully

Just after the college entrance examination, I was confused and didn't know what to do? Tell me what I think
随机推荐
How to learn to spend money
2022年全国最新消防设施操作员(初级消防设施操作员)题库及答案
C language learning record 6
Solve the circular dependency bug. Relying upon circular references is discouraged and they are prohibited by default.
cadence SPB17.4 - allegro - allegro_ free_ viewer
Easyexcel configuration and Application
d区间到可空转换
[the path of system analyst] collection of wrong topics of system analyst
Just after the college entrance examination, I was confused and didn't know what to do? Tell me what I think
Vi LXD deployment of lab server for development records
代码对比工具,我就用这6个
优化调度(火电、风、储能)(Matlab实现)
How to manage the server to make the website stable and smooth
SQL:如何用采购单销售单的数据 通过移动加权平均法 计算商品成本
高比例风电电力系统储能运行及配置分析(Matlab实现)
Application choreography nomad vs. kubernetes
Can't understand kotlin source code? Starting with the contracts function~
2022.2.27 library management system 3 - book borrowing and returning registration module
CVPR 2022 | 神经辐射场几何编辑方法NeRF-Editing
tp6基于whoops的异常接管(漂亮的界面)