File tree Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Original file line number Diff line number Diff line change
1
+ ```
2
+ class Solution {
3
+ public boolean isSymmetric(TreeNode treeNode) {
4
+ if (treeNode == null) {
5
+ return true;
6
+ }else {
7
+ return isSymmetric(treeNode.getLeft(), treeNode.getRight());
8
+ }
9
+ }
10
+
11
+ public boolean isSymmetric(TreeNode left, TreeNode right) {
12
+ if (left == null && right == null) {
13
+ return true;
14
+ }
15
+ if (left == null || right == null) {
16
+ return false;
17
+ }
18
+ if (left.getVal() != right.getVal()) {
19
+ return false;
20
+ }
21
+ return isSymmetric(left.getLeft(), right.getRight()) && isSymmetric(left.getRight(), right.getLeft());
22
+ }
23
+ }
24
+ class TreeNode{
25
+ private int val;
26
+ private TreeNode left;
27
+ private TreeNode right;
28
+
29
+ public TreeNode(int val, TreeNode left, TreeNode right) {
30
+ this.val = val;
31
+ this.left = left;
32
+ this.right = right;
33
+ }
34
+
35
+ public int getVal() {
36
+ return val;
37
+ }
38
+
39
+ public void setVal(int val) {
40
+ this.val = val;
41
+ }
42
+
43
+ public TreeNode getLeft() {
44
+ return left;
45
+ }
46
+
47
+ public void setLeft(TreeNode left) {
48
+ this.left = left;
49
+ }
50
+
51
+ public TreeNode getRight() {
52
+ return right;
53
+ }
54
+
55
+ public void setRight(TreeNode right) {
56
+ this.right = right;
57
+ }
58
+ }
Original file line number Diff line number Diff line change
1
+ > 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。
2
+ > 如果不存在最后一个单词,请返回 0 。
3
+ > 说明:一个单词是指由字母组成,但不包含任何空格的字符串。
4
+
5
+ ```
6
+ public static int lengthOfLastWord(String str){
7
+ String trim = str.trim();
8
+ if (str.matches("[a-zA-Z ]*") && !trim.isEmpty()) {
9
+ int index = trim.lastIndexOf(' ');
10
+ return index < 0 ? trim.length() : trim.length() - index - 1;
11
+ }
12
+ return 0;
13
+ }
You can’t perform that action at this time.
0 commit comments