Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ad401d9

Browse files
authored
feat: add rust solution to lc problem: No.0214 (doocs#1642)
No.0214.Shortest Palindrome
1 parent 91b76bf commit ad401d9

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

‎solution/0200-0299/0214.Shortest Palindrome/README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,32 @@ func shortestPalindrome(s string) string {
161161
}
162162
```
163163

164+
### **Rust**
165+
166+
```rust
167+
impl Solution {
168+
pub fn shortest_palindrome(s: String) -> String {
169+
let base = 131;
170+
let (mut idx, mut prefix, mut suffix, mut mul) = (0, 0, 0, 1);
171+
for (i, c) in s.chars().enumerate() {
172+
let t = c as u64 - '0' as u64 + 1;
173+
prefix = prefix * base + t;
174+
suffix = suffix + t * mul;
175+
mul *= base;
176+
if prefix == suffix {
177+
idx = i + 1;
178+
}
179+
}
180+
if idx == s.len() {
181+
s
182+
} else {
183+
let x: String = (&s[idx..]).chars().rev().collect();
184+
String::from(x + &s)
185+
}
186+
}
187+
}
188+
```
189+
164190
### **...**
165191

166192
```

‎solution/0200-0299/0214.Shortest Palindrome/README_EN.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,32 @@ func shortestPalindrome(s string) string {
132132
}
133133
```
134134

135+
### **Rust**
136+
137+
```rust
138+
impl Solution {
139+
pub fn shortest_palindrome(s: String) -> String {
140+
let base = 131;
141+
let (mut idx, mut prefix, mut suffix, mut mul) = (0, 0, 0, 1);
142+
for (i, c) in s.chars().enumerate() {
143+
let t = c as u64 - '0' as u64 + 1;
144+
prefix = prefix * base + t;
145+
suffix = suffix + t * mul;
146+
mul *= base;
147+
if prefix == suffix {
148+
idx = i + 1;
149+
}
150+
}
151+
if idx == s.len() {
152+
s
153+
} else {
154+
let x: String = (&s[idx..]).chars().rev().collect();
155+
String::from(x + &s)
156+
}
157+
}
158+
}
159+
```
160+
135161
### **...**
136162

137163
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn shortest_palindrome(s: String) -> String {
3+
let base = 131;
4+
let (mut idx, mut prefix, mut suffix, mut mul) = (0, 0, 0, 1);
5+
for (i, c) in s.chars().enumerate() {
6+
let t = c as u64 - '0' as u64 + 1;
7+
prefix = prefix * base + t;
8+
suffix = suffix + t * mul;
9+
mul *= base;
10+
if prefix == suffix {
11+
idx = i + 1;
12+
}
13+
}
14+
if idx == s.len() {
15+
s
16+
} else {
17+
let x: String = (&s[idx..]).chars().rev().collect();
18+
String::from(x + &s)
19+
}
20+
}
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /