Re: [閒聊] 每日leetcode

作者: oin1104 (是oin的說)   2024-10-08 16:08:39
1963.
題目:
有一群[ ]
你可以交換他們的位子
最少幾次交換可以讓他左右括號都匹配
思路:
用two pointer在兩邊紀錄左右括號的數量
有缺的話就去其他地方拿並記錄次數
```cpp
class Solution {
public:
int minSwaps(string s)
{
int res = 0;
int n = s.size();
int l = 0 ;
int r = n-1;
int lcnt = 0;
int rcnt = 0;
while(l<r)
{
if(s[l] == '[')lcnt ++;
else lcnt

Links booklink

Contact Us: admin [ a t ] ucptt.com