File tree 5 files changed +62
-4
lines changed
5 files changed +62
-4
lines changed Original file line number Diff line number Diff line change @@ -135,6 +135,24 @@ public:
135
135
};
136
136
```
137
137
138
+ ### ** Rust**
139
+
140
+ ``` rust
141
+ impl Solution {
142
+ pub fn cutting_rope (mut n : i32 ) -> i32 {
143
+ if n < 4 {
144
+ return n - 1 ;
145
+ }
146
+ let mut res = 1 ;
147
+ while n > 4 {
148
+ res *= 3 ;
149
+ n -= 3 ;
150
+ }
151
+ res * n
152
+ }
153
+ }
154
+ ```
155
+
138
156
### ** ...**
139
157
140
158
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn cutting_rope ( mut n : i32 ) -> i32 {
3
+ if n < 4 {
4
+ return n - 1 ;
5
+ }
6
+ let mut res = 1 ;
7
+ while n > 4 {
8
+ res *= 3 ;
9
+ n -= 3 ;
10
+ }
11
+ res * n
12
+ }
13
+ }
Original file line number Diff line number Diff line change @@ -131,13 +131,29 @@ public:
131
131
ans = ans * 3 % mod;
132
132
n -= 3;
133
133
}
134
- if (n == 4)
135
- return ans * 4 % mod;
136
134
return ans * n % mod;
137
135
}
138
136
};
139
137
```
140
138
139
+ ### **Rust**
140
+
141
+ ```rust
142
+ impl Solution {
143
+ pub fn cutting_rope(mut n: i32) -> i32 {
144
+ if n < 4 {
145
+ return n - 1;
146
+ }
147
+ let mut res = 1i64;
148
+ while n > 4 {
149
+ res = (res * 3) % 1000000007;
150
+ n -= 3;
151
+ }
152
+ ((res * n as i64) % 1000000007) as i32
153
+ }
154
+ }
155
+ ```
156
+
141
157
### ** ...**
142
158
143
159
```
Original file line number Diff line number Diff line change @@ -8,8 +8,6 @@ class Solution {
8
8
ans = ans * 3 % mod;
9
9
n -= 3 ;
10
10
}
11
- if (n == 4 )
12
- return ans * 4 % mod;
13
11
return ans * n % mod;
14
12
}
15
13
};
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn cutting_rope ( mut n : i32 ) -> i32 {
3
+ if n < 4 {
4
+ return n - 1 ;
5
+ }
6
+ let mut res = 1i64 ;
7
+ while n > 4 {
8
+ res = ( res * 3 ) % 1000000007 ;
9
+ n -= 3 ;
10
+ }
11
+ ( ( res * n as i64 ) % 1000000007 ) as i32
12
+ }
13
+ }
You can’t perform that action at this time.
0 commit comments