Skip to content

Commit 6e598c8

Browse files
committed
feat: add solutions to lc problems: No.1689, 2236
- No.1689.Partitioning Into Minimum Number Of Deci-Binary Numbers - No.2236.Root Equals Sum of Children
1 parent 83ebc1b commit 6e598c8

File tree

8 files changed

+284
-64
lines changed

8 files changed

+284
-64
lines changed

solution/1600-1699/1689.Partitioning Into Minimum Number Of Deci-Binary Numbers/README.md

+39-10
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,6 @@ class Solution {
7575
}
7676
```
7777

78-
### **TypeScript**
79-
80-
```ts
81-
function minPartitions(n: string): number {
82-
let nums = n.split('').map(d => parseInt(d));
83-
let ans = Math.max(...nums);
84-
return ans;
85-
}
86-
```
87-
8878
### **C++**
8979

9080
```cpp
@@ -115,6 +105,45 @@ func minPartitions(n string) int {
115105
}
116106
```
117107

108+
### **TypeScript**
109+
110+
```ts
111+
function minPartitions(n: string): number {
112+
let nums = n.split('').map(d => parseInt(d));
113+
let ans = Math.max(...nums);
114+
return ans;
115+
}
116+
```
117+
118+
### **Rust**
119+
120+
```rust
121+
impl Solution {
122+
pub fn min_partitions(n: String) -> i32 {
123+
let mut ans = 0;
124+
for c in n.as_bytes() {
125+
ans = ans.max((c - b'0') as i32);
126+
}
127+
ans
128+
}
129+
}
130+
```
131+
132+
### **C**
133+
134+
```c
135+
int minPartitions(char *n) {
136+
int ans = 0;
137+
for (int i = 0; n[i]; i++) {
138+
int v = n[i] - '0';
139+
if (v > ans) {
140+
ans = v;
141+
}
142+
}
143+
return ans;
144+
}
145+
```
146+
118147
### **...**
119148
120149
```

solution/1600-1699/1689.Partitioning Into Minimum Number Of Deci-Binary Numbers/README_EN.md

+39-10
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ class Solution {
6666
}
6767
```
6868

69-
### **TypeScript**
70-
71-
```ts
72-
function minPartitions(n: string): number {
73-
let nums = n.split('').map(d => parseInt(d));
74-
let ans = Math.max(...nums);
75-
return ans;
76-
}
77-
```
78-
7969
### **C++**
8070

8171
```cpp
@@ -106,6 +96,45 @@ func minPartitions(n string) int {
10696
}
10797
```
10898

99+
### **TypeScript**
100+
101+
```ts
102+
function minPartitions(n: string): number {
103+
let nums = n.split('').map(d => parseInt(d));
104+
let ans = Math.max(...nums);
105+
return ans;
106+
}
107+
```
108+
109+
### **Rust**
110+
111+
```rust
112+
impl Solution {
113+
pub fn min_partitions(n: String) -> i32 {
114+
let mut ans = 0;
115+
for c in n.as_bytes() {
116+
ans = ans.max((c - b'0') as i32);
117+
}
118+
ans
119+
}
120+
}
121+
```
122+
123+
### **C**
124+
125+
```c
126+
int minPartitions(char *n) {
127+
int ans = 0;
128+
for (int i = 0; n[i]; i++) {
129+
int v = n[i] - '0';
130+
if (v > ans) {
131+
ans = v;
132+
}
133+
}
134+
return ans;
135+
}
136+
```
137+
109138
### **...**
110139
111140
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int minPartitions(char *n) {
2+
int ans = 0;
3+
for (int i = 0; n[i]; i++) {
4+
int v = n[i] - '0';
5+
if (v > ans) {
6+
ans = v;
7+
}
8+
}
9+
return ans;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
impl Solution {
2+
pub fn min_partitions(n: String) -> i32 {
3+
let mut ans = 0;
4+
for c in n.as_bytes() {
5+
ans = ans.max((c - b'0') as i32);
6+
}
7+
ans
8+
}
9+
}

solution/2200-2299/2236.Root Equals Sum of Children/README.md

+73-22
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,6 @@ class Solution {
8888
}
8989
```
9090

91-
### **TypeScript**
92-
93-
```ts
94-
/**
95-
* Definition for a binary tree node.
96-
* class TreeNode {
97-
* val: number
98-
* left: TreeNode | null
99-
* right: TreeNode | null
100-
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
101-
* this.val = (val===undefined ? 0 : val)
102-
* this.left = (left===undefined ? null : left)
103-
* this.right = (right===undefined ? null : right)
104-
* }
105-
* }
106-
*/
107-
108-
function checkTree(root: TreeNode | null): boolean {
109-
return root.val === root.left.val + root.right.val;
110-
}
111-
```
112-
11391
### **C++**
11492

11593
```cpp
@@ -148,6 +126,79 @@ func checkTree(root *TreeNode) bool {
148126
}
149127
```
150128

129+
### **TypeScript**
130+
131+
```ts
132+
/**
133+
* Definition for a binary tree node.
134+
* class TreeNode {
135+
* val: number
136+
* left: TreeNode | null
137+
* right: TreeNode | null
138+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
139+
* this.val = (val===undefined ? 0 : val)
140+
* this.left = (left===undefined ? null : left)
141+
* this.right = (right===undefined ? null : right)
142+
* }
143+
* }
144+
*/
145+
146+
function checkTree(root: TreeNode | null): boolean {
147+
return root.val === root.left.val + root.right.val;
148+
}
149+
```
150+
151+
### **Rust**
152+
153+
```rust
154+
// Definition for a binary tree node.
155+
// #[derive(Debug, PartialEq, Eq)]
156+
// pub struct TreeNode {
157+
// pub val: i32,
158+
// pub left: Option<Rc<RefCell<TreeNode>>>,
159+
// pub right: Option<Rc<RefCell<TreeNode>>>,
160+
// }
161+
//
162+
// impl TreeNode {
163+
// #[inline]
164+
// pub fn new(val: i32) -> Self {
165+
// TreeNode {
166+
// val,
167+
// left: None,
168+
// right: None
169+
// }
170+
// }
171+
// }
172+
use std::rc::Rc;
173+
use std::cell::RefCell;
174+
impl Solution {
175+
pub fn check_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
176+
let node = root.as_ref().unwrap().borrow();
177+
let left = node.left.as_ref().unwrap().borrow().val;
178+
let right = node.right.as_ref().unwrap().borrow().val;
179+
node.val == left + right
180+
}
181+
}
182+
```
183+
184+
### **C**
185+
186+
```c
187+
/**
188+
* Definition for a binary tree node.
189+
* struct TreeNode {
190+
* int val;
191+
* struct TreeNode *left;
192+
* struct TreeNode *right;
193+
* };
194+
*/
195+
196+
197+
bool checkTree(struct TreeNode *root) {
198+
return root->val == root->left->val + root->right->val;
199+
}
200+
```
201+
151202
### **...**
152203
153204
```

solution/2200-2299/2236.Root Equals Sum of Children/README_EN.md

+73-22
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,6 @@ class Solution {
7878
}
7979
```
8080

81-
### **TypeScript**
82-
83-
```ts
84-
/**
85-
* Definition for a binary tree node.
86-
* class TreeNode {
87-
* val: number
88-
* left: TreeNode | null
89-
* right: TreeNode | null
90-
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
91-
* this.val = (val===undefined ? 0 : val)
92-
* this.left = (left===undefined ? null : left)
93-
* this.right = (right===undefined ? null : right)
94-
* }
95-
* }
96-
*/
97-
98-
function checkTree(root: TreeNode | null): boolean {
99-
return root.val === root.left.val + root.right.val;
100-
}
101-
```
102-
10381
### **C++**
10482

10583
```cpp
@@ -138,6 +116,79 @@ func checkTree(root *TreeNode) bool {
138116
}
139117
```
140118

119+
### **TypeScript**
120+
121+
```ts
122+
/**
123+
* Definition for a binary tree node.
124+
* class TreeNode {
125+
* val: number
126+
* left: TreeNode | null
127+
* right: TreeNode | null
128+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
129+
* this.val = (val===undefined ? 0 : val)
130+
* this.left = (left===undefined ? null : left)
131+
* this.right = (right===undefined ? null : right)
132+
* }
133+
* }
134+
*/
135+
136+
function checkTree(root: TreeNode | null): boolean {
137+
return root.val === root.left.val + root.right.val;
138+
}
139+
```
140+
141+
### **Rust**
142+
143+
```rust
144+
// Definition for a binary tree node.
145+
// #[derive(Debug, PartialEq, Eq)]
146+
// pub struct TreeNode {
147+
// pub val: i32,
148+
// pub left: Option<Rc<RefCell<TreeNode>>>,
149+
// pub right: Option<Rc<RefCell<TreeNode>>>,
150+
// }
151+
//
152+
// impl TreeNode {
153+
// #[inline]
154+
// pub fn new(val: i32) -> Self {
155+
// TreeNode {
156+
// val,
157+
// left: None,
158+
// right: None
159+
// }
160+
// }
161+
// }
162+
use std::rc::Rc;
163+
use std::cell::RefCell;
164+
impl Solution {
165+
pub fn check_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
166+
let node = root.as_ref().unwrap().borrow();
167+
let left = node.left.as_ref().unwrap().borrow().val;
168+
let right = node.right.as_ref().unwrap().borrow().val;
169+
node.val == left + right
170+
}
171+
}
172+
```
173+
174+
### **C**
175+
176+
```c
177+
/**
178+
* Definition for a binary tree node.
179+
* struct TreeNode {
180+
* int val;
181+
* struct TreeNode *left;
182+
* struct TreeNode *right;
183+
* };
184+
*/
185+
186+
187+
bool checkTree(struct TreeNode *root) {
188+
return root->val == root->left->val + root->right->val;
189+
}
190+
```
191+
141192
### **...**
142193
143194
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
11+
bool checkTree(struct TreeNode *root) {
12+
return root->val == root->left->val + root->right->val;
13+
}

0 commit comments

Comments
 (0)