Skip to content

Commit ae56156

Browse files
committedFeb 27, 2022
feat: add solutions to lc problem: No.0553
No.0553.Optimal Division
1 parent 621f327 commit ae56156

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
 

‎solution/0500-0599/0553.Optimal Division/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,45 @@ func optimalDivision(nums []int) string {
125125
}
126126
```
127127

128+
### **TypeScript**
129+
130+
```ts
131+
function optimalDivision(nums: number[]): string {
132+
const n = nums.length;
133+
const res = nums.join('/');
134+
if (n > 2) {
135+
const index = res.indexOf('/') + 1;
136+
return `${res.slice(0, index)}(${res.slice(index)})`;
137+
}
138+
return res;
139+
}
140+
```
141+
142+
### **Rust**
143+
144+
```rust
145+
impl Solution {
146+
pub fn optimal_division(nums: Vec<i32>) -> String {
147+
let n = nums.len();
148+
match n {
149+
1 => nums[0].to_string(),
150+
2 => nums[0].to_string() + "/" + &nums[1].to_string(),
151+
_ => {
152+
let mut res = nums[0].to_string();
153+
res.push_str("/(");
154+
for i in 1..n {
155+
res.push_str(&nums[i].to_string());
156+
res.push('/');
157+
}
158+
res.pop();
159+
res.push(')');
160+
res
161+
}
162+
}
163+
}
164+
}
165+
```
166+
128167
### **...**
129168

130169
```

‎solution/0500-0599/0553.Optimal Division/README_EN.md

+39
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,45 @@ func optimalDivision(nums []int) string {
133133
}
134134
```
135135

136+
### **TypeScript**
137+
138+
```ts
139+
function optimalDivision(nums: number[]): string {
140+
const n = nums.length;
141+
const res = nums.join('/');
142+
if (n > 2) {
143+
const index = res.indexOf('/') + 1;
144+
return `${res.slice(0, index)}(${res.slice(index)})`;
145+
}
146+
return res;
147+
}
148+
```
149+
150+
### **Rust**
151+
152+
```rust
153+
impl Solution {
154+
pub fn optimal_division(nums: Vec<i32>) -> String {
155+
let n = nums.len();
156+
match n {
157+
1 => nums[0].to_string(),
158+
2 => nums[0].to_string() + "/" + &nums[1].to_string(),
159+
_ => {
160+
let mut res = nums[0].to_string();
161+
res.push_str("/(");
162+
for i in 1..n {
163+
res.push_str(&nums[i].to_string());
164+
res.push('/');
165+
}
166+
res.pop();
167+
res.push(')');
168+
res
169+
}
170+
}
171+
}
172+
}
173+
```
174+
136175
### **...**
137176

138177
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn optimal_division(nums: Vec<i32>) -> String {
3+
let n = nums.len();
4+
match n {
5+
1 => nums[0].to_string(),
6+
2 => nums[0].to_string() + "/" + &nums[1].to_string(),
7+
_ => {
8+
let mut res = nums[0].to_string();
9+
res.push_str("/(");
10+
for i in 1..n {
11+
res.push_str(&nums[i].to_string());
12+
res.push('/');
13+
}
14+
res.pop();
15+
res.push(')');
16+
res
17+
}
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function optimalDivision(nums: number[]): string {
2+
const n = nums.length;
3+
const res = nums.join('/');
4+
if (n > 2) {
5+
const index = res.indexOf('/') + 1;
6+
return `${res.slice(0, index)}(${res.slice(index)})`;
7+
}
8+
return res;
9+
}

0 commit comments

Comments
 (0)