8.8拼多多笔试

第三题,提交上去一直0%
笔试之后发现之前‘D’的情况有问题,改了代码,有没有大佬帮忙看看对不对呀?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String s = br.readLine();
        char[] cs = s.toCharArray();
        StringBuilder sb = new StringBuilder();
        int maxDeth = 0, currentDeth = 0;
        int leftNum = 0, rightNum = 0;
        int index = 0;
        for (char c : cs) {
            if (c == '(') {
                currentDeth++;
                leftNum++;
                sb.insert(index, '(');
                index++;
            } else if (c == ')') {
                currentDeth--;
                rightNum++;
                sb.insert(index, ')');
                index++;
            } else if (c == 'L') {
                if (index > 0) {
                    index--;
                    if (sb.charAt(index) == '(') {
                        currentDeth--;
                    } else if (sb.charAt(index) == ')') {
                        currentDeth++;
                    }
                }
            } else if (c == 'R') {
                if (index < sb.length()) {
                    if (sb.charAt(index) == '(') {
                        currentDeth++;
                    } else if (sb.charAt(index) == ')') {
                        currentDeth--;
                    }
                    index++;
                }
            } else if (c == 'D') {
                if (index > 0) {
                    index--;
                    if (sb.charAt(index) == '(') {
                        currentDeth--;
                        leftNum--;
                        //提交时maxDeth有问题
                        if (index == 0 || sb.charAt(index-1) == '(') {
                            maxDeth--;
                        }
                    } else if (sb.charAt(index) == ')') {
                        currentDeth++;
                        rightNum--;
                    }
                    sb.deleteCharAt(index);
                }
            }

            maxDeth = Math.max(maxDeth, currentDeth);
            if (rightNum == leftNum) {
                System.out.print(maxDeth+" ");
            } else {
                System.out.print(Math.abs(rightNum-leftNum)*(-1) + " ");
            }
        }
    }
}


#拼多多笔试##拼多多##笔试题目#
全部评论
求问第四题的n最大是多少?
点赞 回复 分享
发布于 2021-08-08 21:52

相关推荐

评论
1
1
分享
牛客网
牛客企业服务