当前位置:网站首页>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);
}
}
}
}
边栏推荐
- 7 RSA Cryptosystem
- 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)
- [211] go handles the detailed documents of Excel library
- How to improve development quality
- [system analyst's road] Chapter 7 double disk system design (structured development method)
- "In Vietnam, money is like lying on the street"
- 【211】go 处理excel的库的详细文档
- 【Hot100】32. 最长有效括号
- SIGMOD’22 HiEngine论文解读
- 机器学习概念漂移检测方法(Aporia)
猜你喜欢
78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
Why are some online concerts always weird?
蓝桥:合根植物
明星开店,退,退,退
“在越南,钱就像躺在街上”
华为云ModelArts的使用教程(附详细图解)
uni-app与uviewUI实现仿小米商城app(附源码)
Blue bridge: sympodial plant
Self reflection of a small VC after two years of entrepreneurship
机器学习概念漂移检测方法(Aporia)
随机推荐
爬虫初级学习
[209] go language learning ideas
Numpy 的仿制 2
Is it safe to open an account online? is that true?
The block:usdd has strong growth momentum
【210】PHP 定界符的用法
【Hot100】31. 下一个排列
Pytorch深度学习之环境搭建
【Hot100】32. Longest valid bracket
表情包坑惨职场人
Clever use of curl command
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
Set the transparent hidden taskbar and full screen display of the form
Win32 API 访问路由的加密网页
regular expression
超标量处理器设计 姚永斌 第6章 指令解码 摘录
内核中时间相关的知识介绍
【系统盘转回U盘】记录系统盘转回U盘的操作
Is it safe to download the mobile version of Anxin securities and open an account online
高中物理:力、物体和平衡