当前位置:网站首页>力扣刷题日记/day7/6.30
力扣刷题日记/day7/6.30
2022-07-04 16:33:00 【bobo洁厕灵】
新手村
当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。
和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。
因为java的字符串是不可以修改的,字符串拼接操作时,会生成临时的字符串副本,并在使用后销毁
在使用 StringBuffer 类时,每次都会对 StringBuffer 对象本身进行操作,而不是生成新的对象,所以如果需要对字符串进行修改推荐使用 StringBuffer。
StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。
由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。
//创建一个StringBuilder对象
StringBuilder sb = new StringBuilder(10);
//创建一个StringBuffer对象
StringBuffer sBuffer = new StringBuffer("菜鸟教程官网:");
//将内容加到sBuffer字符串后
sBbuffer.append("内容");
//toString() 方法用于返回以一个字符串表示的 Number 对象值
System.out.println(new Object().toString());
常用方法:
Java StringBuffer 和 StringBuilder 类 | 菜鸟教程 (runoob.com)
Java toString() 方法 | 菜鸟教程 (runoob.com)
二叉树的遍历
深度优先遍历(DFS)
解题思路:利用递归,深度优先遍历的思想
考虑节点和节点的孩子节点
若节点是叶子节点,在已经搜索到的路径的末尾添加该节点,得到一条从根节点到叶子节点的路径,将该路径直接加入返回变量即可
若节点不是叶子节点,在已经搜索到路径的末尾添加该节点,并继续递归遍历该节点的每一个孩子节点
遍历完整个二叉树,就得到了所有的从根节点到叶子节点的路径
/**
* 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 List<String> binaryTreePaths(TreeNode root) {
List<String> paths = new ArrayList<String>(); //定义一个返回变量paths
constructPaths(root,"",paths); //递归执行深度优先搜索
return paths;
}
public void constructPaths(TreeNode root,String path,List<String> paths){
if(root != null){ //判断节点是否存在
StringBuffer pathSB = new StringBuffer(path); //定义一个pathSB用来存储临时路径
pathSB.append(String.valueOf(root.val)); //将节点值加入路径的末尾
if(root.left == null && root.right == null){ //如果是叶子节点
paths.add(pathSB.toString()); //将pathSB转换成String对象加入到返回变量paths中
} else{
pathSB.append("->"); //不是叶子节点,就在临时路径后加->
constructPaths(root.left,pathSB.toString(),paths); //继续搜索其子节点
constructPaths(root.right,pathSB.toString(),paths);
}
}
}
}
边栏推荐
- I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
- [211] go handles the detailed documents of Excel library
- 中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
- 12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
- Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
- Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
- 超标量处理器设计 姚永斌 第5章 指令集体系 摘录
- Numpy 的仿制 2
- 明星开店,退,退,退
- Pytorch深度学习之环境搭建
猜你喜欢
90后开始攒钱植发,又一个IPO来了
[HCIA continuous update] WAN technology
“在越南,钱就像躺在街上”
Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
How to improve development quality
Recast of recastnavigation
With an estimated value of 90billion, the IPO of super chip is coming
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
Machine learning concept drift detection method (Apria)
随机推荐
2022 national CMMI certification subsidy policy | Changxu consulting
Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像
How to improve development quality
Set the transparent hidden taskbar and full screen display of the form
[daily question] 871 Minimum refueling times
Initial experience of domestic database tidb: simple and easy to use, quick to start
Heartless sword Chinese translation of Elizabeth Bishop's a skill
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
High school physics: force, object and balance
内核中时间相关的知识介绍
android使用SQLiteOpenHelper闪退
【Hot100】32. Longest valid bracket
Why are some online concerts always weird?
俄罗斯 Arenadata 发布基于PostgreSQL的产品
Weima, which is going to be listed, still can't give Baidu confidence
With an estimated value of 90billion, the IPO of super chip is coming
Thawte通配符SSL证书提供的类型有哪些
明星开店,退,退,退