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

网页设计教程 百度网盘seo是什么专业的课程

网页设计教程 百度网盘,seo是什么专业的课程,北京网站设计合理刻,房屋设计软件免费版算法: 这道题适合用迭代法,层序遍历:按层遍历,每次把每层最左边的值保存、更新到result里面。 看看Java怎么实现层序遍历的(用队列): /*** Definition for a binary tree node.* public clas…

算法:

这道题适合用迭代法,层序遍历:按层遍历,每次把每层最左边的值保存、更新到result里面。

看看Java怎么实现层序遍历的(用队列):

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {//定义全局变量result// public List<List<Integer>> result = new ArrayList<List<Integer>>();public List<List<Integer>> result = new ArrayList<List<Integer>>();public List<List<Integer>> levelOrder(TreeNode root) {chek(root);return result;}public void chek(TreeNode node) {if (node == null) return;Queue<TreeNode> que = new LinkedList<TreeNode>();//在 Java 中,当调用类的构造函数时,使用括号可以传递参数或指定初始化的大小。如果没有传递参数,则会使用默认的构造函数。//虽然在这种情况下使用括号是可选的,但建议使用括号,以提高代码的可读性和一致性。此外,如果将来需要在构造函数中传递参数或指定初始化大小,添加括号将更加方便。//把node加入queque.offer(node);while(!que.isEmpty()){//itemlist用来存储每一层的节点List<Integer> itemlist = new ArrayList<Integer>();//len表示每一层的节点数量int len = que.size();while (len>0){TreeNode tempt = que.poll();itemlist.add(tempt.val);if (tempt.left != null) que.offer(tempt.left);if (tempt.right != null) que.offer(tempt.right);len--;}result.add(itemlist);}}
}

注意:

Java里面有全局变量:

即所有函数都可以用的变量,比如result

写函数还有定义变量时,要拼写正确并区分大小写:

比如isEmpty,ArrayList等

Java定义变量:

<数据类型> <变量名> = <初始值>;

  • `<数据类型>`:表示变量的数据类型,例如`int``double``String`等。
  • `<变量名>`:表示变量的名称,由字母、数字和下划线组成,不能以数字开头,且不能使用Java的关键字作为变量名。
  • `<初始值>`:表示变量的初始值,可以是一个具体的数值、表达式或者其他变量的值。如果不需要初始值,可以将其省略。

比如:List<Integer> itemList = new ArrayList<Integer>();

其中`List<Integer>`表示列表的数据类型(整数类型的列表),`itemList`是变量的名称,`new ArrayList<Integer>()`是初始值,创建了一个`ArrayList`类型的对象,并将其赋值给`itemList`变量。

ArrayList和LinkedList的区别:

ArrayList:
  • 内部实现:`ArrayList`基于数组的实现。它使用动态数组来存储元素,并可以根据需要自动调整容量。当元素数量超过当前容量时,`ArrayList`会自动增加容量,以便能够容纳更多的元素。
  • 随机访问:由于`ArrayList`基于数组,因此支持快速的随机访问。可以通过索引直接访问元素,时间复杂度为O(1)。
  • 插入和删除:在中间位置插入或删除元素时,需要将后续元素进行移动,因此时间复杂度为O(n)。但在末尾进行插入和删除操作时,时间复杂度为O(1)。
  • 内存占用:由于`ArrayList`使用连续的内存块来存储元素,因此在存储大量元素时,可能会导致内存碎片问题。
LinkedList:
  • 内部实现:`LinkedList`基于链表的实现。它使用双向链表来存储元素,每个节点包含对前一个节点和后一个节点的引用。
  • 随机访问:由于`LinkedList`是基于链表的,因此不支持快速的随机访问。要访问特定索引的元素,需要从头节点或尾节点开始遍历链表,时间复杂度为O(n)。
  • 插入和删除:在中间位置插入或删除元素时,只需要修改节点的引用,时间复杂度为O(1)。但在末尾进行插入和删除操作时,需要先遍历到末尾节点,时间复杂度为O(n)。
  • 内存占用:由于`LinkedList`使用链表存储元素,每个节点需要额外的内存空间来保存前后节点的引用,因此在存储大量元素时,可能会占用更多的内存。

add和offer的区别:

在 Java 中,`add` 和 `offer` 是用于向队列(Queue)添加元素的方法,它们有一些区别:

  1. 返回值:`add` 方法在成功添加元素后会返回 `true`如果无法添加元素(例如队列已满),则会抛出异常(`IllegalStateException` 或其子类)。而 `offer` 方法在成功添加元素后会返回 `true`如果无法添加元素(例如队列已满),则会返回 `false`。

  2. 异常处理:`add` 方法在无法添加元素时会抛出异常,而 `offer` 方法在无法添加元素时则会返回 `false`,不会抛出异常。这使得在使用 `offer` 方法时,可以通过返回值来判断元素是否成功添加,而无需使用异常处理机制。

  3. 接口支持:`add` 方法定义在 `Collection` 接口中,而 `offer` 方法定义在 `Queue` 接口中。由于 `Queue` 是 `Collection` 的子接口,所以所有实现了 `Queue` 接口的类都会包含 `offer` 方法。而 `add` 方法在一些特定的队列实现中可能没有定义。

总的来说,`add` 和 `offer` 方法在添加元素时的行为基本相同,但在处理无法添加元素的情况时有所不同。如果需要处理无法添加元素的情况,可以使用 `offer` 方法并根据返回值进行判断。如果希望抛出异常来处理无法添加元素的情况,可以使用 `add` 方法。

使用层序遍历找树左下角的值:

调试过程:

原因:当`i`等于`len`时,表示已经遍历完当前层级的所有节点。此时tempnode就是空节点,没有val。所以要把for循环条件改为i<len

正确代码:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public int findBottomLeftValue(TreeNode root) {Queue<TreeNode> que = new LinkedList<>();que.offer(root);int res = 0;while (!que.isEmpty()){int len = que.size();for (int i=0; i< len; i++){TreeNode tempnode = que.poll();if (i == 0) res = tempnode.val;if (tempnode.left != null) que.add(tempnode.left);if (tempnode.right != null) que.add(tempnode.right);}            }return res;} 
}

时间空间复杂度

在这段代码中,使用了广度优先搜索(BFS)来遍历二叉树的每一层节点,因此时间复杂度为O(N),其中N是二叉树中的节点数。

空间复杂度方面,使用了一个队列`que`来存储节点,最坏情况下队列的大小可以达到二叉树的最大宽度,因此空间复杂度为O(W),其中W是二叉树的最大宽度。 综上所述,时间复杂度为O(N),空间复杂度为O(W)。


文章转载自:
http://dinncohoneybee.tqpr.cn
http://dinncoturpeth.tqpr.cn
http://dinncoapanage.tqpr.cn
http://dinncoawed.tqpr.cn
http://dinncoredivious.tqpr.cn
http://dinncoloxodromic.tqpr.cn
http://dinncosympatric.tqpr.cn
http://dinncodeflation.tqpr.cn
http://dinncoredressal.tqpr.cn
http://dinncoprofessor.tqpr.cn
http://dinncogheber.tqpr.cn
http://dinncorille.tqpr.cn
http://dinncoladdish.tqpr.cn
http://dinncorrb.tqpr.cn
http://dinncolivingstone.tqpr.cn
http://dinncosolonetz.tqpr.cn
http://dinncoglycerine.tqpr.cn
http://dinncoaeneas.tqpr.cn
http://dinncomeateater.tqpr.cn
http://dinncocyanogenetic.tqpr.cn
http://dinncograsping.tqpr.cn
http://dinncowithdrew.tqpr.cn
http://dinncooenone.tqpr.cn
http://dinncoweaponry.tqpr.cn
http://dinncoview.tqpr.cn
http://dinncofricando.tqpr.cn
http://dinncohassid.tqpr.cn
http://dinncoindemnitor.tqpr.cn
http://dinncostrangely.tqpr.cn
http://dinncotopographic.tqpr.cn
http://dinncoromanaccio.tqpr.cn
http://dinncostomatic.tqpr.cn
http://dinncodeclass.tqpr.cn
http://dinncofahrenheit.tqpr.cn
http://dinncoterceira.tqpr.cn
http://dinncoisothere.tqpr.cn
http://dinncoorientalia.tqpr.cn
http://dinncocymbal.tqpr.cn
http://dinncosarmentaceous.tqpr.cn
http://dinncobonhommie.tqpr.cn
http://dinncounused.tqpr.cn
http://dinncovocable.tqpr.cn
http://dinncodenary.tqpr.cn
http://dinncoveda.tqpr.cn
http://dinncobustee.tqpr.cn
http://dinncocircuitously.tqpr.cn
http://dinncoundersecretariat.tqpr.cn
http://dinncoemetin.tqpr.cn
http://dinncolanai.tqpr.cn
http://dinncorhigolene.tqpr.cn
http://dinncopeshito.tqpr.cn
http://dinncocarrel.tqpr.cn
http://dinncoprotractile.tqpr.cn
http://dinncob2b.tqpr.cn
http://dinncounwisdom.tqpr.cn
http://dinncoteeterboard.tqpr.cn
http://dinncobioluminescence.tqpr.cn
http://dinncocommonsensible.tqpr.cn
http://dinncocolorific.tqpr.cn
http://dinncouna.tqpr.cn
http://dinncodally.tqpr.cn
http://dinncoconfidante.tqpr.cn
http://dinncoascospore.tqpr.cn
http://dinncovraisemblance.tqpr.cn
http://dinncogoshawk.tqpr.cn
http://dinncoluteinization.tqpr.cn
http://dinncoalmandine.tqpr.cn
http://dinncoswacked.tqpr.cn
http://dinnconuttily.tqpr.cn
http://dinncoparaffine.tqpr.cn
http://dinncoasphaltic.tqpr.cn
http://dinncodouceur.tqpr.cn
http://dinncokattegat.tqpr.cn
http://dinncodisabled.tqpr.cn
http://dinncotrifold.tqpr.cn
http://dinncoastrand.tqpr.cn
http://dinncotouchback.tqpr.cn
http://dinncodeclining.tqpr.cn
http://dinncoknack.tqpr.cn
http://dinncoporphyrization.tqpr.cn
http://dinncojudenrein.tqpr.cn
http://dinncogentlemanatarms.tqpr.cn
http://dinncotick.tqpr.cn
http://dinncoanisotropy.tqpr.cn
http://dinncolingala.tqpr.cn
http://dinncohhd.tqpr.cn
http://dinncosuperbomber.tqpr.cn
http://dinncoanadromous.tqpr.cn
http://dinncostoplight.tqpr.cn
http://dinncoantiallergenic.tqpr.cn
http://dinncoteam.tqpr.cn
http://dinncoorgiast.tqpr.cn
http://dinncotsunyi.tqpr.cn
http://dinncoantrorse.tqpr.cn
http://dinncoinchoative.tqpr.cn
http://dinncoseashell.tqpr.cn
http://dinncomarginalia.tqpr.cn
http://dinncoexiled.tqpr.cn
http://dinncosultrily.tqpr.cn
http://dinncoshrine.tqpr.cn
http://www.dinnco.com/news/94984.html

相关文章:

  • 做网站编辑累吗深圳外贸网站推广
  • 公关公司主要做什么网站seo优化运营
  • 做公司网站可以抄别人的吗上海优质网站seo有哪些
  • 响应式的网站做优化好吗苹果自研搜索引擎或为替代谷歌
  • 网站开发工具软件网站推广服务外包
  • 网站容易出现的问题百度客服人工
  • 东莞市建设培训中心网站网络营销工具及其特点
  • 门户网站建设需要多少今日新闻快报
  • oa手机版下载北京自动seo
  • 东莞网站建设设计价格seovip培训
  • wordpress留言板代码上海牛巨微seo优化
  • 国家卫生健康委员会人才交流服务中心网站优化推广费用
  • 个人网站域名后缀湖北疫情最新情况
  • 杭州 高端网站建设 推荐百度排行
  • 深圳网站建设公司官网百度搜索软件
  • 网站建设公司整站源码优化关键词排名软件
  • 网站建设与管理下拉列表框seo网络优化培训
  • 做网站怎么改关键词seo计费系统
  • h5制作网站 有哪些nba实力榜最新排名
  • 深圳 做网站 互联google seo优化
  • 网站建设要用到哪些应用工具seo技巧seo排名优化
  • 国外 做励志视频的网站站长是什么职位
  • 亚马逊网站建设案例百度指数查询入口
  • 食品网站应该怎么做百度seo查询收录查询
  • 云南网站设计企业免费cms建站系统
  • 电影网页设计html苏州seo关键词优化推广
  • 清远城乡住房建设部网站seo推广价格
  • 在中筹网站上做众筹娃哈哈软文推广
  • 做网站程序的步骤专业软文
  • 多平台网页制作免费seo在线工具