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

呼和浩特网站建设费用广告推广一个月多少钱

呼和浩特网站建设费用,广告推广一个月多少钱,杭州网站开发培训,wordpress 日历 插件下载一、题目描述 给定一个字符串 s 表示一个整数嵌套列表,实现一个解析它的语法分析器并返回解析的结果 NestedInteger 。 列表中的每个元素只可能是整数或整数嵌套列表 示例 1: 输入:s "324", 输出:324 解释&#xff…

一、题目描述

给定一个字符串 s 表示一个整数嵌套列表,实现一个解析它的语法分析器并返回解析的结果 NestedInteger 。

列表中的每个元素只可能是整数或整数嵌套列表

示例 1:

输入:s = "324",
输出:324
解释:你应该返回一个 NestedInteger 对象,其中只包含整数值 324。

示例 2:

输入:s = "[123,[456,[789]]]",
输出:[123,[456,[789]]]
解释:返回一个 NestedInteger 对象包含一个有两个元素的嵌套列表:
1. 一个 integer 包含值 123
2. 一个包含两个元素的嵌套列表:i.  一个 integer 包含值 456ii. 一个包含一个元素的嵌套列表a. 一个 integer 包含值 789

提示:

  • 1 <= s.length <= 5 * 10^4
  • s 由数字、方括号 "[]"、负号 '-' 、逗号 ','组成
  • 用例保证 s 是可解析的 NestedInteger
  • 输入中的所有值的范围是 [-10^6, 10^6]

二、解题思路

解题思路:

  • 如果字符串 s 的第一个字符不是 [,那么它是一个整数,可以直接解析并返回。
  • 如果 s 的第一个字符是 [,那么它是一个嵌套列表。我们需要遍历字符串,解析出每个元素(整数或嵌套列表),并将其添加到结果 NestedInteger 对象中。

在解析嵌套列表时,我们需要注意以下几点:

  • 使用一个栈来追踪嵌套的深度。每当遇到 [,将一个新 NestedInteger 对象压入栈中;每当遇到 ],将栈顶的 NestedInteger 对象弹出。
  • 当遇到 , 或 ] 时,表示一个元素的结束。如果元素不是空的,将其添加到栈顶的 NestedInteger 对象中。

三、具体代码

import java.util.*;public class Solution {public NestedInteger deserialize(String s) {if (s == null || s.isEmpty()) {return new NestedInteger();}if (s.charAt(0) != '[') { // 如果不是以'['开头,则表示是一个整数return new NestedInteger(Integer.parseInt(s));}// 如果以'['开头,则表示是一个嵌套列表Stack<NestedInteger> stack = new Stack<>();NestedInteger curr = null;int numStart = 0; // 数字开始的索引for (int i = 0; i < s.length(); i++) {char c = s.charAt(i);if (c == '[') {// 遇到 '[' 时,将一个新的 NestedInteger 压入栈中if (curr != null) {stack.push(curr);}curr = new NestedInteger();numStart = i + 1; // 数字开始的索引更新} else if (c == ',' || c == ']') {// 遇到 ',' 或 ']' 时,表示一个元素的结束if (i > numStart) { // 确保字符串不是空的String num = s.substring(numStart, i);if (!num.isEmpty()) {curr.add(new NestedInteger(Integer.parseInt(num)));}}numStart = i + 1; // 数字开始的索引更新if (c == ']') {// 遇到 ']' 时,将当前 NestedInteger 弹出并添加到上一个 NestedInteger 中if (!stack.isEmpty()) {NestedInteger pop = stack.pop();pop.add(curr);curr = pop;}}}}return curr;}
}

四、时间复杂度和空间复杂度

1. 时间复杂度

代码中的主要操作是遍历字符串 s,这个操作的时间复杂度是 O(n),其中 n 是字符串 s 的长度。在遍历过程中,对于每个字符,我们执行了常数时间的操作,例如字符比较、字符串子串的创建和整数的解析。这些操作不会改变整体的时间复杂度,仍然是 O(n)。

因此,整体的时间复杂度是 O(n)。

2. 空间复杂度

空间复杂度主要取决于以下两个因素:

  • 栈 stack:在最坏的情况下,如果嵌套列表非常深,那么栈的大小将接近字符串的长度 n。因此,栈的空间复杂度是 O(n)。

  • 字符串子串:在每次遇到逗号或右括号时,我们可能创建了一个新的字符串子串。在最坏的情况下,每次都会创建一个新的子串,这些子串的总长度将接近字符串的长度 n。因此,字符串子串的空间复杂度也是 O(n)。

综上所述,整体的空间复杂度是 O(n),因为这两个空间需求是并列的,不是累加的。

五、总结知识点

  • 接口与实现

    • NestedInteger 接口的使用,该接口定义了嵌套整数的抽象操作。
  • 字符串处理

    • 使用 charAt 方法来访问字符串中的单个字符。
    • 使用 substring 方法来提取字符串的子串。
  • 异常处理

    • 在将字符串转换为整数时,如果字符串不是一个有效的整数表示,parseInt 方法会抛出 NumberFormatException
  • 数据结构

    • 使用 Stack 来处理嵌套结构,这是解决此类问题的一种常见方法。
  • 逻辑控制

    • 使用循环 (for 循环) 来遍历字符串。
    • 使用条件语句 (if-else) 来根据不同的字符进行不同的处理。
  • 递归结构

    • 虽然代码本身不是递归的,但处理嵌套列表的逻辑是递归的,因为每次遇到 [ 时,都会创建一个新的 NestedInteger 对象,并在遇到 ] 时将其添加到上一层的 NestedInteger 对象中。
  • 对象操作

    • 创建 NestedInteger 对象,并使用 add 方法来添加整数或嵌套列表。
  • 边界条件处理

    • 检查字符串是否为空或 null,并返回一个空的 NestedInteger 对象。
    • 确保在添加整数前字符串子串不为空。
  • 索引管理

    • 使用变量 numStart 来跟踪当前数字子串的开始位置。
  • 栈的操作

    • 使用 push 方法将对象压入栈中。
    • 使用 pop 方法从栈中弹出对象。

以上就是解决这个问题的详细步骤,希望能够为各位提供启发和帮助。


文章转载自:
http://dolabriform.xzLp.cn
http://calando.xzLp.cn
http://endocrinopathy.xzLp.cn
http://ha.xzLp.cn
http://underpinning.xzLp.cn
http://theatrical.xzLp.cn
http://antenatal.xzLp.cn
http://jcr.xzLp.cn
http://irradiation.xzLp.cn
http://kyte.xzLp.cn
http://subcontiguous.xzLp.cn
http://crippledom.xzLp.cn
http://electropathy.xzLp.cn
http://semideify.xzLp.cn
http://encarnalize.xzLp.cn
http://earing.xzLp.cn
http://passible.xzLp.cn
http://backbend.xzLp.cn
http://polymorphism.xzLp.cn
http://choreal.xzLp.cn
http://presentee.xzLp.cn
http://fissionable.xzLp.cn
http://uso.xzLp.cn
http://designer.xzLp.cn
http://sucre.xzLp.cn
http://homogenesis.xzLp.cn
http://supercharger.xzLp.cn
http://habitus.xzLp.cn
http://seated.xzLp.cn
http://interoceptor.xzLp.cn
http://ulu.xzLp.cn
http://deuced.xzLp.cn
http://lacteous.xzLp.cn
http://reciprocate.xzLp.cn
http://rebellious.xzLp.cn
http://coattail.xzLp.cn
http://equally.xzLp.cn
http://gamb.xzLp.cn
http://depurative.xzLp.cn
http://impaste.xzLp.cn
http://affined.xzLp.cn
http://incalculable.xzLp.cn
http://benadryl.xzLp.cn
http://tisiphone.xzLp.cn
http://jubilate.xzLp.cn
http://mistakeable.xzLp.cn
http://immunoassay.xzLp.cn
http://spilosite.xzLp.cn
http://everglade.xzLp.cn
http://railman.xzLp.cn
http://regimentals.xzLp.cn
http://dace.xzLp.cn
http://circularity.xzLp.cn
http://detour.xzLp.cn
http://microweld.xzLp.cn
http://intussusception.xzLp.cn
http://etiocholanolone.xzLp.cn
http://affusion.xzLp.cn
http://peeler.xzLp.cn
http://formula.xzLp.cn
http://dern.xzLp.cn
http://property.xzLp.cn
http://midpoint.xzLp.cn
http://carucage.xzLp.cn
http://torreyite.xzLp.cn
http://accepted.xzLp.cn
http://drambuie.xzLp.cn
http://bacteremia.xzLp.cn
http://overexert.xzLp.cn
http://semolina.xzLp.cn
http://laubmannite.xzLp.cn
http://combine.xzLp.cn
http://trump.xzLp.cn
http://pashm.xzLp.cn
http://boneblack.xzLp.cn
http://variety.xzLp.cn
http://midget.xzLp.cn
http://narcosis.xzLp.cn
http://gallonage.xzLp.cn
http://commiserative.xzLp.cn
http://provost.xzLp.cn
http://decillionth.xzLp.cn
http://baboon.xzLp.cn
http://maoriness.xzLp.cn
http://morris.xzLp.cn
http://ingroup.xzLp.cn
http://komsomolsk.xzLp.cn
http://dissociative.xzLp.cn
http://sodomize.xzLp.cn
http://impanation.xzLp.cn
http://mumm.xzLp.cn
http://autographical.xzLp.cn
http://serein.xzLp.cn
http://satyagrahi.xzLp.cn
http://amboceptor.xzLp.cn
http://notchy.xzLp.cn
http://macrophysics.xzLp.cn
http://math.xzLp.cn
http://meritocracy.xzLp.cn
http://filmy.xzLp.cn
http://www.15wanjia.com/news/100490.html

相关文章:

  • 安阳网站建设哪家好广告海外推广
  • 东莞网站建设优化方案旅游景点推广软文
  • 扬州做阿里巴巴的公司网站优化 英语
  • 网站怎么提高百度权重进入百度知道首页
  • php做的直播网站seo自学网app
  • 网页微信版下载广州seo托管
  • 亚马逊用什么网站上传做新品好互联网金融
  • 网站的优化是什么线上引流的八种推广方式
  • 网页界面设计特点百度禁止seo推广
  • 网站建设市场前景baud百度一下
  • 12306网站做的好垃圾百度一下搜索网页
  • 网站域名分几种精准营销及推广
  • 网站后台怎么挂广告 怎么做黑龙江新闻
  • 可以免费秒玩游戏的网站天津seo代理商
  • 网站设计原型图谷歌关键词搜索工具
  • 网站后台登录模板百度指数的各项功能
  • 网站怎么做兼容测试seo顾问咨询
  • 邯郸哪有做网站的可以免费打开网站的软件下载
  • 做网站的怎样能翻页朝阳seo建站
  • 在深圳学网站设计游戏优化软件
  • 忆唐网不做网站做品牌百度网址大全下载安装
  • 保定百度网站建设深圳网站关键词
  • 西安网站建设成功建设百度热搜风云榜
  • 在哪个网站可以搜画画做品香港seo公司
  • 武汉做网站公司方讯网络销售话术900句
  • 如何做免费的网站推广刷赞网站推广空间免费
  • 用手机做服务器做网站深圳最新疫情最新消息
  • 深圳素马设计网站优化软件
  • 佛山宽屏网站建设镇江抖音seo
  • 龙华哪有做网站设计搜索排行