当前位置: 首页 > news >正文

Java | Leetcode Java题解之第385题迷你语法分析器

题目:

题解:

class Solution {public NestedInteger deserialize(String s) {if (s.charAt(0) != '[') {return new NestedInteger(Integer.parseInt(s));}Deque<NestedInteger> stack = new ArrayDeque<NestedInteger>();int num = 0;boolean negative = false;for (int i = 0; i < s.length(); i++) {char c = s.charAt(i);if (c == '-') {negative = true;} else if (Character.isDigit(c)) {num = num * 10 + c - '0';} else if (c == '[') {stack.push(new NestedInteger());} else if (c == ',' || c == ']') {if (Character.isDigit(s.charAt(i - 1))) {if (negative) {num *= -1;}stack.peek().add(new NestedInteger(num));}num = 0;negative = false;if (c == ']' && stack.size() > 1) {NestedInteger ni = stack.pop();stack.peek().add(ni);}}}return stack.pop();}
}

http://www.mrgr.cn/news/19981.html

相关文章:

  • 【Google Play】X浏览器4.8.1国际版(如何鉴别是否官方?)
  • python文件自动分类(3)
  • flowable 国产数据库并发性能优化
  • 5G农业大数据中心顶层设计
  • 链表.......
  • 我完成第一个AI项目的全过程记录——对IMDB电影评论的情感分析
  • wps匹配加入数据
  • 使用python+opencv解析图像和文本数据
  • 5.6对数变换和非对数变换
  • token和cookie的区别以及接口测试怎么找接口中的token和cookie
  • Java异常关键字
  • 从数据到洞察:DB-GPT驱动的智能化财报分析解决方案
  • 出现 TypeError: Cannot read properties of undefined (reading ‘getUserMedia‘) 解决方法
  • lnmp - tp6.0的安装和简单使用
  • 代码生成器技术乱弹五十五,动词算子再正名
  • AI学习指南深度学习篇-SGD的变种算法
  • 力扣215.返回数组中第K个最大元素
  • Python教程(十九):使用Tkinter制作计算器
  • 基于web的古玩网站设计与实现
  • 欺诈文本分类检测(十):QLora量化微调