Skip to content

Commit 7fd80af

Browse files
committed
feat: add solutions to lc problems: No.0110,0459
- No.0110.Balanced Binary Tree - No.0459.Repeated Substring Pattern
1 parent c6d87b0 commit 7fd80af

File tree

8 files changed

+318
-0
lines changed

8 files changed

+318
-0
lines changed

solution/0100-0199/0110.Balanced Binary Tree/README.md

+76
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,82 @@ func max(a, b int) int {
217217
}
218218
```
219219

220+
### **TypeScript**
221+
222+
```ts
223+
/**
224+
* Definition for a binary tree node.
225+
* class TreeNode {
226+
* val: number
227+
* left: TreeNode | null
228+
* right: TreeNode | null
229+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
230+
* this.val = (val===undefined ? 0 : val)
231+
* this.left = (left===undefined ? null : left)
232+
* this.right = (right===undefined ? null : right)
233+
* }
234+
* }
235+
*/
236+
237+
function isBalanced(root: TreeNode | null): boolean {
238+
const dfs = (root: TreeNode | null) => {
239+
if (root == null) {
240+
return 0;
241+
}
242+
const left = dfs(root.left);
243+
const right = dfs(root.right);
244+
if (left === -1 || right === -1 || Math.abs(left - right) > 1) {
245+
return -1;
246+
}
247+
return 1 + Math.max(left, right);
248+
};
249+
return dfs(root) > -1;
250+
}
251+
```
252+
253+
### **Rust**
254+
255+
```rust
256+
// Definition for a binary tree node.
257+
// #[derive(Debug, PartialEq, Eq)]
258+
// pub struct TreeNode {
259+
// pub val: i32,
260+
// pub left: Option<Rc<RefCell<TreeNode>>>,
261+
// pub right: Option<Rc<RefCell<TreeNode>>>,
262+
// }
263+
//
264+
// impl TreeNode {
265+
// #[inline]
266+
// pub fn new(val: i32) -> Self {
267+
// TreeNode {
268+
// val,
269+
// left: None,
270+
// right: None
271+
// }
272+
// }
273+
// }
274+
use std::rc::Rc;
275+
use std::cell::RefCell;
276+
impl Solution {
277+
pub fn is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
278+
Self::dfs(&root) > -1
279+
}
280+
281+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
282+
if root.is_none() {
283+
return 0;
284+
}
285+
let node = root.as_ref().unwrap().borrow();
286+
let left = Self::dfs(&node.left);
287+
let right = Self::dfs(&node.right);
288+
if left == -1 || right == -1 || (left - right).abs() > 1 {
289+
return -1;
290+
}
291+
1 + left.max(right)
292+
}
293+
}
294+
```
295+
220296
### **...**
221297

222298
```

solution/0100-0199/0110.Balanced Binary Tree/README_EN.md

+76
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,82 @@ func max(a, b int) int {
207207
}
208208
```
209209

210+
### **TypeScript**
211+
212+
```ts
213+
/**
214+
* Definition for a binary tree node.
215+
* class TreeNode {
216+
* val: number
217+
* left: TreeNode | null
218+
* right: TreeNode | null
219+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
220+
* this.val = (val===undefined ? 0 : val)
221+
* this.left = (left===undefined ? null : left)
222+
* this.right = (right===undefined ? null : right)
223+
* }
224+
* }
225+
*/
226+
227+
function isBalanced(root: TreeNode | null): boolean {
228+
const dfs = (root: TreeNode | null) => {
229+
if (root == null) {
230+
return 0;
231+
}
232+
const left = dfs(root.left);
233+
const right = dfs(root.right);
234+
if (left === -1 || right === -1 || Math.abs(left - right) > 1) {
235+
return -1;
236+
}
237+
return 1 + Math.max(left, right);
238+
};
239+
return dfs(root) > -1;
240+
}
241+
```
242+
243+
### **Rust**
244+
245+
```rust
246+
// Definition for a binary tree node.
247+
// #[derive(Debug, PartialEq, Eq)]
248+
// pub struct TreeNode {
249+
// pub val: i32,
250+
// pub left: Option<Rc<RefCell<TreeNode>>>,
251+
// pub right: Option<Rc<RefCell<TreeNode>>>,
252+
// }
253+
//
254+
// impl TreeNode {
255+
// #[inline]
256+
// pub fn new(val: i32) -> Self {
257+
// TreeNode {
258+
// val,
259+
// left: None,
260+
// right: None
261+
// }
262+
// }
263+
// }
264+
use std::rc::Rc;
265+
use std::cell::RefCell;
266+
impl Solution {
267+
pub fn is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
268+
Self::dfs(&root) > -1
269+
}
270+
271+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
272+
if root.is_none() {
273+
return 0;
274+
}
275+
let node = root.as_ref().unwrap().borrow();
276+
let left = Self::dfs(&node.left);
277+
let right = Self::dfs(&node.right);
278+
if left == -1 || right == -1 || (left - right).abs() > 1 {
279+
return -1;
280+
}
281+
1 + left.max(right)
282+
}
283+
}
284+
```
285+
210286
### **...**
211287

212288
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
pub fn is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
23+
Self::dfs(&root) > -1
24+
}
25+
26+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
27+
if root.is_none() {
28+
return 0;
29+
}
30+
let node = root.as_ref().unwrap().borrow();
31+
let left = Self::dfs(&node.left);
32+
let right = Self::dfs(&node.right);
33+
if left == -1 || right == -1 || (left - right).abs() > 1 {
34+
return -1;
35+
}
36+
1 + left.max(right)
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function isBalanced(root: TreeNode | null): boolean {
16+
const dfs = (root: TreeNode | null) => {
17+
if (root == null) {
18+
return 0;
19+
}
20+
const left = dfs(root.left);
21+
const right = dfs(root.right);
22+
if (left === -1 || right === -1 || Math.abs(left - right) > 1) {
23+
return -1;
24+
}
25+
return 1 + Math.max(left, right);
26+
};
27+
return dfs(root) > -1;
28+
}

solution/0400-0499/0459.Repeated Substring Pattern/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,53 @@
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```java
66+
class Solution {
67+
public boolean repeatedSubstringPattern(String s) {
68+
String str = s + s;
69+
return str.substring(1, str.length() - 1).contains(s);
70+
}
71+
}
72+
```
73+
74+
### **TypeScript**
75+
76+
```ts
77+
function repeatedSubstringPattern(s: string): boolean {
78+
return (s + s).slice(1, (s.length << 1) - 1).includes(s);
79+
}
80+
```
81+
82+
```ts
83+
function repeatedSubstringPattern(s: string): boolean {
84+
const n = s.length;
85+
for (let i = 0; i < n >> 1; i++) {
86+
const len = i + 1;
87+
if (n % len !== 0) {
88+
continue;
89+
}
90+
const t = s.slice(0, len);
91+
let j: number;
92+
for (j = len; j < n; j += len) {
93+
if (s.slice(j, j + len) !== t) {
94+
break;
95+
}
96+
}
97+
if (j === n) {
98+
return true;
99+
}
100+
}
101+
return false;
102+
}
103+
```
104+
105+
### **Rust**
66106

107+
```rust
108+
impl Solution {
109+
pub fn repeated_substring_pattern(s: String) -> bool {
110+
(s.clone() + &s)[1..s.len() * 2 - 1].contains(&s)
111+
}
112+
}
67113
```
68114

69115
### **...**

solution/0400-0499/0459.Repeated Substring Pattern/README_EN.md

+46
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,53 @@
5151
### **Java**
5252

5353
```java
54+
class Solution {
55+
public boolean repeatedSubstringPattern(String s) {
56+
String str = s + s;
57+
return str.substring(1, str.length() - 1).contains(s);
58+
}
59+
}
60+
```
61+
62+
### **TypeScript**
63+
64+
```ts
65+
function repeatedSubstringPattern(s: string): boolean {
66+
return (s + s).slice(1, (s.length << 1) - 1).includes(s);
67+
}
68+
```
69+
70+
```ts
71+
function repeatedSubstringPattern(s: string): boolean {
72+
const n = s.length;
73+
for (let i = 0; i < n >> 1; i++) {
74+
const len = i + 1;
75+
if (n % len !== 0) {
76+
continue;
77+
}
78+
const t = s.slice(0, len);
79+
let j: number;
80+
for (j = len; j < n; j += len) {
81+
if (s.slice(j, j + len) !== t) {
82+
break;
83+
}
84+
}
85+
if (j === n) {
86+
return true;
87+
}
88+
}
89+
return false;
90+
}
91+
```
92+
93+
### **Rust**
5494

95+
```rust
96+
impl Solution {
97+
pub fn repeated_substring_pattern(s: String) -> bool {
98+
(s.clone() + &s)[1..s.len() * 2 - 1].contains(&s)
99+
}
100+
}
55101
```
56102

57103
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn repeated_substring_pattern(s: String) -> bool {
3+
(s.clone() + &s)[1..s.len() * 2 - 1].contains(&s)
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function repeatedSubstringPattern(s: string): boolean {
2+
return (s + s).slice(1, (s.length << 1) - 1).includes(s);
3+
}

0 commit comments

Comments
 (0)