当前位置:网站首页>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);
}
}
}
}
边栏推荐
- DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
- ARTS_ twenty million two hundred and twenty thousand six hundred and twenty-eight
- uni-app与uviewUI实现仿小米商城app(附源码)
- 网上开户安全吗?是真的吗?
- Reptile elementary learning
- Numpy 的仿制 2
- 比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
- 华为云ModelArts的使用教程(附详细图解)
- The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
- 【209】go语言的学习思想
猜你喜欢
Mysql5.7 installation tutorial graphic explanation
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
Once the "king of color TV", he sold pork before delisting
删除二叉搜索树中的节点附图详解
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
“在越南,钱就像躺在街上”
数学分析_笔记_第7章:多元函数的微分学
The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
明星开店,退,退,退
随机推荐
数学分析_笔记_第7章:多元函数的微分学
用于图数据库的开源 PostgreSQL 扩展 AGE被宣布为 Apache 软件基金会顶级项目
Summary of subsidy policies across the country for dcmm certification in 2022
Grain Mall (I)
Is BigDecimal safe to calculate the amount? Look at these five pits~~
Large scale service exception log retrieval
网上开户安全吗?是真的吗?
Blue bridge: sympodial plant
Android uses sqliteopenhelper to flash back
Mysql5.7 installation tutorial graphic explanation
You should know something about ci/cd
fopen、fread、fwrite、fseek 的文件处理示例
五千字讲清楚团队自组织建设 | Liga 妙谈
Redis master-slave replication
[system disk back to U disk] record the operation of system disk back to U disk
ITSS运维能力成熟度分级详解|一文搞清ITSS证书
被忽视的问题:测试环境配置管理
Self reflection of a small VC after two years of entrepreneurship
General environmental instructions for the project
删除二叉搜索树中的节点附图详解