-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 字典序最小回文串 | ||
// https://leetcode.cn/problems/lexicographically-smallest-palindrome | ||
// INLINE ../../images/string/lexicographically_smallest_palindrome.jpeg | ||
|
||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn make_smallest_palindrome(s: String) -> String { | ||
let mut s: Vec<char> = s.chars().collect(); | ||
let mut left = 0; | ||
let mut right = s.len() - 1; | ||
|
||
while left < right { | ||
if s[left] != s[right] { | ||
s[left] = std::cmp::min(s[left], s[right]); | ||
s[right] = std::cmp::min(s[left], s[right]); | ||
} | ||
left += 1; | ||
right -= 1; | ||
} | ||
|
||
s.into_iter().collect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/string/lexicographically_smallest_palindrome_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use rust_practice::string::lexicographically_smallest_palindrome::Solution; | ||
|
||
#[test] | ||
fn make_smallest_palindrome() { | ||
// 示例 1: | ||
// 输入:s = "egcfe" | ||
// 输出:"efcfe" | ||
// 解释:将 "egcfe" 变成回文字符串的最小操作次数为 1 ,修改 1 次得到的字典序最小回文字符串是 "efcfe",只需将 'g' 改为 'f' 。 | ||
let s = "egcfe".to_string(); | ||
assert_eq!(Solution::make_smallest_palindrome(s), "efcfe".to_string()); | ||
|
||
// 示例 2: | ||
// 输入:s = "abcd" | ||
// 输出:"abba" | ||
// 解释:将 "abcd" 变成回文字符串的最小操作次数为 2 ,修改 2 次得到的字典序最小回文字符串是 "abba" 。 | ||
let s = "abcd".to_string(); | ||
assert_eq!(Solution::make_smallest_palindrome(s), "abba".to_string()); | ||
|
||
// 示例 3: | ||
// 输入:s = "seven" | ||
// 输出:"neven" | ||
// 解释:将 "seven" 变成回文字符串的最小操作次数为 1 ,修改 1 次得到的字典序最小回文字符串是 "neven" 。 | ||
let s = "seven".to_string(); | ||
assert_eq!(Solution::make_smallest_palindrome(s), "neven".to_string()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters