当前位置:网站首页>力扣刷题日记/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);
}
}
}
}
边栏推荐
- 庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
- 比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
- Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
- Talk about seven ways to realize asynchronous programming
- Self reflection of a small VC after two years of entrepreneurship
- LD_LIBRARY_PATH 环境变量设置
- Face_ Attendance statistics of recognition face recognition
- [system disk back to U disk] record the operation of system disk back to U disk
- LD_ LIBRARY_ Path environment variable setting
- Machine learning concept drift detection method (Apria)
猜你喜欢
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
Thawte通配符SSL证书提供的类型有哪些
Is it science or metaphysics to rename a listed company?
使用3DMAX制作一枚手雷
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
90后开始攒钱植发,又一个IPO来了
创业两年,一家小VC的自我反思
[HCIA continuous update] network management and operation and maintenance
上市公司改名,科学还是玄学?
随机推荐
Make a grenade with 3DMAX
MySQL common add, delete, modify and query operations (crud)
【210】PHP 定界符的用法
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
如何提高开发质量
New technology releases a small program UNIPRO to meet customers' mobile office scenarios
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
TCP waves twice, have you seen it? What about four handshakes?
2022 national CMMI certification subsidy policy | Changxu consulting
Stars open stores, return, return, return
Analysis of I2C adapter driver of s5pv210 chip (i2c-s3c2410. C)
Redis master-slave replication
[proteus simulation] printf debugging output example based on VSM serial port
使用3DMAX制作一枚手雷
MVC mode and three-tier architecture
The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
Is it science or metaphysics to rename a listed company?
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
mysql5.7安装教程图文详解
Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)