当前位置:网站首页>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);
}
}
}
}
边栏推荐
- TCP waves twice, have you seen it? What about four handshakes?
- 【Hot100】31. Next spread
- How to test MDM products
- [209] go language learning ideas
- mysql5.7安装教程图文详解
- Is it science or metaphysics to rename a listed company?
- The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
- 基于NCF的多模块协同实例
- 【每日一题】556. 下一个更大元素 III
- Pytorch深度学习之环境搭建
猜你喜欢

People in the workplace with a miserable expression

简单易用的地图可视化

With an estimated value of 90billion, the IPO of super chip is coming

Numpy 的仿制 2

uni-app与uviewUI实现仿小米商城app(附源码)

RecastNavigation 之 Recast

Reptile elementary learning

The controversial line of energy replenishment: will fast charging lead to reunification?

蓝桥:合根植物

mysql5.7安装教程图文详解
随机推荐
[HCIA continuous update] network management and operation and maintenance
uni-app与uviewUI实现仿小米商城app(附源码)
怎么开户才是安全的,
Achieve animation effect through event binding
Heartless sword Chinese translation of Elizabeth Bishop's a skill
创业两年,一家小VC的自我反思
fopen、fread、fwrite、fseek 的文件处理示例
Numpy 的仿制 2
【209】go语言的学习思想
Why are some online concerts always weird?
Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
Performance test of Gatling
网上开户安全吗?是真的吗?
Is it safe to open an account online? is that true?
android使用SQLiteOpenHelper闪退
The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
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)
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
【Proteus仿真】基于VSM 串口printf调试输出示例