Skip to content

Commit 36f3a87

Browse files
committed
feat: add rust solution to lcof problems: No.14 - |,14 - ||
- 面试题14- I. 剪绳子 - 面试题14- II. 剪绳子 II
1 parent 59b6e96 commit 36f3a87

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

lcof/面试题14- I. 剪绳子/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ public:
135135
};
136136
```
137137

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+
138156
### **...**
139157

140158
```
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

lcof/面试题14- II. 剪绳子 II/README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,29 @@ public:
131131
ans = ans * 3 % mod;
132132
n -= 3;
133133
}
134-
if (n == 4)
135-
return ans * 4 % mod;
136134
return ans * n % mod;
137135
}
138136
};
139137
```
140138
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+
141157
### **...**
142158

143159
```

lcof/面试题14- II. 剪绳子 II/Solution.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ class Solution {
88
ans = ans * 3 % mod;
99
n -= 3;
1010
}
11-
if (n == 4)
12-
return ans * 4 % mod;
1311
return ans * n % mod;
1412
}
1513
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

0 commit comments

Comments
 (0)