当前位置:网站首页>Li Kou brush question diary /day7/6.30
Li Kou brush question diary /day7/6.30
2022-07-04 18:23:00 【Bobo toilet cleaning spirit】
Novice Village
When the Modify the string When , Need to use StringBuffer and StringBuilder class .
and String Class is different from ,StringBuffer and StringBuilder Class can be modified many times , And don't generate new unused objects .
because java The string cannot be modified , String splicing operation , A temporary copy of the string will be generated , And destroy it after use
In the use of StringBuffer Class time , Every time I was right about StringBuffer The object itself operates , Instead of generating new objects , So if The string needs to be modified. It is recommended to use StringBuffer.
StringBuilder Class in Java 5 It was proposed that , It and StringBuffer The biggest difference between them is StringBuilder Is not thread safe ( Can't sync access ).
because StringBuilder Compare with StringBuffer Speed advantage , So in most cases, it is recommended to use StringBuilder class .
// Create a StringBuilder object
StringBuilder sb = new StringBuilder(10);
// Create a StringBuffer object
StringBuffer sBuffer = new StringBuffer(" Rookie tutorial website :");
// Add content to sBuffer After the string
sBbuffer.append(" Content ");
//toString() Method is used to return a string representing Number The object is worth
System.out.println(new Object().toString());Common methods :
Java StringBuffer and StringBuilder class | Novice tutorial (runoob.com)
Java toString() Method | Novice tutorial (runoob.com)
Traversal of binary tree
Depth-first traversal (DFS)

Their thinking : Recursion , The idea of depth first traversal
Consider nodes and child nodes of nodes
If the node is a leaf node , Add this node at the end of the path that has been searched , Get a path from the root node to the leaf node , Add this path directly to the return variable
If the node is not a leaf node , Add this node at the end of the path that has been searched , And continue to recursively traverse every child node of the node
Traverse the entire binary tree , You get all the paths from the root node to the leaf node
/**
* 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>(); // Define a return variable paths
constructPaths(root,"",paths); // Recursively perform a depth first search
return paths;
}
public void constructPaths(TreeNode root,String path,List<String> paths){
if(root != null){ // Determine whether the node exists
StringBuffer pathSB = new StringBuffer(path); // Define a pathSB Used to store temporary paths
pathSB.append(String.valueOf(root.val)); // Add the node value to the end of the path
if(root.left == null && root.right == null){ // If it's a leaf node
paths.add(pathSB.toString()); // take pathSB convert to String Object is added to the return variable paths in
} else{
pathSB.append("->"); // It's not a leaf node , Just add... After the temporary path ->
constructPaths(root.left,pathSB.toString(),paths); // Continue to search its child nodes
constructPaths(root.right,pathSB.toString(),paths);
}
}
}
}
边栏推荐
- 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)
- About the pit of firewall opening 8848 when Nacos is started
- Face_recognition人脸识别之考勤统计
- Grain Mall (I)
- DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
- 通过事件绑定实现动画效果
- Summary of subsidy policies across the country for dcmm certification in 2022
- 股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
- 明星开店,退,退,退
- 上市公司改名,科学还是玄学?
猜你喜欢

上市公司改名,科学还是玄学?

输入的查询SQL语句,是如何执行的?

力扣刷题日记/day8/7.1

.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)

【Hot100】32. 最长有效括号

celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
![[cloud native] what is the](/img/00/0cb0f38bf3eb5dad02b3bc4ead36ba.jpg)
[cloud native] what is the "grid" of service grid?

MVC mode and three-tier architecture

How to test MDM products

Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
随机推荐
用于图数据库的开源 PostgreSQL 扩展 AGE被宣布为 Apache 软件基金会顶级项目
Thawte通配符SSL证书提供的类型有哪些
Lua emmylua annotation details
78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
如何提高开发质量
celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
[HCIA continuous update] network management and operation and maintenance
Stars open stores, return, return, return
MySQL common add, delete, modify and query operations (crud)
无心剑中译伊丽莎白·毕肖普《一门技艺》
Set the transparent hidden taskbar and full screen display of the form
ISO27001 certification process and 2022 subsidy policy summary
正则表达式
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
Win32 API access route encrypted web pages
[proteus simulation] printf debugging output example based on VSM serial port
The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
Is BigDecimal safe to calculate the amount? Look at these five pits~~