猿辅导笔试7.31第二题

猿辅导笔试7.31第二题
自测都能过,提交0%

//
//修改过后
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String boxs = sc.nextLine();
        int n = boxs.length();
        int ret = 0;
        Stack<Integer> c = new Stack<Integer> ();
        c.push(0);
        for (int i = 0; i < n; ++i) {
            // System.out.println(c.toString());
            char ch = boxs.charAt(i);
            if (ch == '[') {
                c.push(1);
            } else if (ch == ']') {
                if (i < n-1 && (Character.isDigit(boxs.charAt(i+1)))) {
                    c.push(c.pop() * (boxs.charAt(i+1) - '0'));
                    i = i+1;
                }
                c.push(c.pop() + c.pop());
            }
        }
        while(!c.empty())
            ret += c.pop();
        System.out.println(ret);
    }
}
// 原不正确代码:



import java.util.Scanner; import java.util.Stack; public class Main {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         String boxs = sc.nextLine();         int n = boxs.length();         int ret = 0;         Stack<Integer> c = new Stack<Integer> ();         Stack<Character> s = new Stack<> ();         c.push(0);         s.push(boxs.charAt(0));         for (int i = 1; i < n; ++i) {             // System.out.println(c.toString());             char ch = boxs.charAt(i);             if (ch == '[') {                 if ( !s.empty() && s.peek() == ']') {                     s.pop();                     s.pop();                     if (!c.empty()) {                         if (c.peek().equals(0)) {                             c.pop();                             c.push(1);                         } else {                             int sum = 0;                             int e;                             while (c.peek() != 0) {                                 e = c.pop();                                 sum += e;                             }                             c.pop();                             c.push(sum + 1);                         }                     }                 }                 c.push(0);                 s.push(ch);             } else if (ch == ']') {                 s.push(ch);             } else {                 if ( !s.empty()) {                     s.pop();                     s.pop();                     if (!c.empty()) {                         if (c.peek().equals(0)) {                             c.pop();                             c.push(ch - '0');                         } else {                             int sum = 0;                             int e;                             while (c.peek() != 0) {                                 e = c.pop();                                 sum += e;                             }                                                          c.pop();                             c.push((sum+1)* (ch - '0'));                         }                     }                 }             }         }         while(!c.empty())              ret += c.pop();         System.out.println(ret);     } }


#猿辅导笔试讨论##笔经##猿辅导#
全部评论
我也是,不知道是不是用栈模拟会超时
点赞 回复 分享
发布于 2021-07-31 20:56
我也是自测都过,提交0%
点赞 回复 分享
发布于 2021-07-31 21:00
你输入 [] , 输出 0, 输入[][], 输出1,你这两个测试用例都有问题
点赞 回复 分享
发布于 2021-07-31 21:22
你的代码有问题[][][][[[]2][]]3输出的是9
点赞 回复 分享
发布于 2021-07-31 22:13
现在还冲教培行业吗
点赞 回复 分享
发布于 2021-07-31 22:54
记不太清题目了,代码没有处理多位数的情况,比如[][][][[[]2][]]13
点赞 回复 分享
发布于 2021-08-01 11:27

相关推荐

不愿透露姓名的神秘牛友
11-27 10:28
点赞 评论 收藏
分享
挣K存W养DOG:他真的很中意你,为什么不回他
点赞 评论 收藏
分享
11-09 12:17
清华大学 C++
out11Man:小丑罢了,不用理会
点赞 评论 收藏
分享
评论
1
1
分享
牛客网
牛客企业服务