首页 > 8.8拼多多笔试
头像
F_QY
发布于 2021-08-08 21:43
+ 关注

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) + " ");
            }
        }
    }
}


全部评论

(1) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐