• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

LeetCode 1963 .使串平衡的最小交换次数


Recommended Posts

原题链接在这里:https://leet代码。com/problems/minimum-使字符串平衡的交换次数/

题目:

给你一个指数为0的字符串。该字符串由十六/2个左括号"["和n/2个右括号"]"组成。

一个字符串叫做平衡if并且只有if:

它是空字符串,或者

它可以写成asAB,其中既有

它可以写成[C],其中独联体是一个平衡字符串。

您可以任意多次交换任意两个索引的括号。

返回要达到平衡的最小交换数量。

示例1:

Input: s='][]['

输出: 1

解释:您可以将索引0与索引3互换,使字符串平衡。

结果字符串是"[[]]"。

示例2:

Input: s=']]][[['

输出: 2

解释:您可以用下列方法来平衡字符串:

-将索引0与索引四交换s='[]][][' .

-将索引一与索引5交换s='[[][]]' .

结果字符串是"[[][]]"。

示例3:

输入: s='[]'

输出: 0

解释:弦已经平衡。

约束:

n==长度

2=n=106

尼斯偶数。

s不是'['就是']'。

左括号"["的数量等于sn/2,右括号"]"的数量等于sn/2 .

题解:

忽略所有实心线对。

找出未售出线对的数量。

最小交换数是计数/2的上限。

时间复杂度北:号.n=标准长度()。

空间: O(1).

AC Java:

一类解决方案{

2个公共int minSwaps(字符串)

3 if(s==null || s.length()==0){

四返回0;

5 }

6

7 int count=0;

8 for(int I=0;国际标准长度();i ){

9 char c=s . charat(I);

10 if(c=='['){

11计数;

12 }否则如果(计数0){

13计数-;

14 }

15 }

16

17返回(计数1)/2;

18 }

19 }

类似使括号有效的最小添加,使括号有效的最小移除.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now