File tree 4 files changed +107
-0
lines changed
solution/0500-0599/0553.Optimal Division
4 files changed +107
-0
lines changed Original file line number Diff line number Diff line change @@ -125,6 +125,45 @@ func optimalDivision(nums []int) string {
125
125
}
126
126
```
127
127
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
+
128
167
### ** ...**
129
168
130
169
```
Original file line number Diff line number Diff line change @@ -133,6 +133,45 @@ func optimalDivision(nums []int) string {
133
133
}
134
134
```
135
135
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
+
136
175
### ** ...**
137
176
138
177
```
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments