缺失的字符串
package week03;
import java.util.Scanner;
public class 缺失的字符串 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String arr = sc.nextLine();
int need = 0;
int count = 0;
for(int i = 0; i < arr.length(); i ++)
{
if(arr.charAt(i) == '(')
{
count++;
}
else
{
count--;
}
if(count < 0)
{
need ++;
count = 0;
}
}
if(count != 0)
{
need = need + count;
}
System.out.println(need);
}
}
需要多少need,我们想如果是count++的话就是(,但是如果count<0的话就是需要一个括号来补全,然后need++,因为补全了所以count=0,然后再去判断
最后的最后还是需要如果都是((的话就是count = 2,也就是说count不是等于的左括号右括号数量相等,所以再用这个来加一下

浙公网安备 33010602011771号