385. Mini Parser - LeetCode

Question

385.?Mini Parser

Solution

分析:用NI(count,list)来表示NestedInteger,则解析字符串[123,[456,[789]]]过程如下:

# 首先将字符串转化为字符数组,遍历每个字符
[      压栈操作 NI(0, null)
123    给栈顶元素设置值 NI(0, NI(123))
,      不处理
[      压栈操作 NI(0, NI(123)) | NI(0, null)
456    给栈顶元素设置值 NI(0, NI(123)) | NI(0, 456)
,      不处理
[      压栈操作 NI(0, NI(123)) | NI(0, NI(456)) | NI(0, null)
789    给栈顶元素设置值 NI(0, NI(123)) | NI(0, NI(456)) | NI(0, NI(789))
]      出栈并将出栈后的元素添加到栈顶元素NI(0, NI(123)) | NI(0, [NI(456),NI(0, NI(789))])
]      出栈并将出栈后的元素添加到栈顶元素NI(0, [NI(123), NI(0, [NI(456), NI(0, NI(789))])])
]      不做处理
# 栈中最后一个元素就是返回结果

Java实现:

public NestedInteger deserialize(String s) {
    if (!s.startsWith("[")) return new NestedInteger(Integer.parseInt(s));
    Stack<NestedInteger> stack = new Stack<>();
    // 123,[456,[789]]

    char[] arr = s.toCharArray();
    for (int i = 0; i < arr.length; i++) {
        char ch = arr[i];
        if (ch == '[') {
            stack.push(new NestedInteger());
        } else if (ch == ']' & stack.size() > 1) {
            NestedInteger pop = stack.pop();
            stack.peek().add(pop);
        } else if (Character.isDigit(ch) || ch == '-') {
            boolean pos = true;
            if (ch == '-') {
                pos = false;
                i++;
            }
            int num = 0;
            while (i < arr.length && Character.isDigit(arr[i])) {
                num *= 10;
                num += arr[i++] - '0';
            }
            i--;
            stack.peek().add(new NestedInteger(pos ? num : -num));
        }
    }
    return stack.pop();
}

Reference

原文地址:https://www.cnblogs.com/okokabcd/p/9175184.html

时间: 2024-10-17 12:06:06

385. Mini Parser - LeetCode的相关文章

【Leetcode】Mini Parser

题目链接:https://leetcode.com/problems/mini-parser/ 题目: Given a nested list of integers represented as a string, implement a parser to deserialize it. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Not

[LeetCode] Mini Parser 迷你解析器

Given a nested list of integers represented as a string, implement a parser to deserialize it. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Note: You may assume that the string is well-formed: St

Leetcode: Mini Parser

Given a nested list of integers represented as a string, implement a parser to deserialize it. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Note: You may assume that the string is well-formed: St

Ternary Expression Parser Leetcode

Given a string representing arbitrarily nested ternary expressions, calculate the result of the expression. You can always assume that the given expression is valid and only consists of digits 0-9, ?, :, T and F (T and F represent True and False resp

Leetcode 前 400 重点 250 题

这个重点题目是把Leetcode前400题进行精简,划分精简规则如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & 389,保留一个) 所有题目尽量保证客观公正,只是按大概率删除不常考题目,很多题目面经出现过, 但出现次数属于个位数或者只有一两家出现进行删除.所以如在面试中出现删除题目概不负责,这只是从概率上删除低频,简单题目. 旨在减轻大家的刷

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.