-
Notifications
You must be signed in to change notification settings - Fork 1
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
5 changed files
with
169 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "lc-01-06-compress-string-lcci" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
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,22 @@ | ||
|
||
# 面试题 01.06. 字符串压缩 | ||
|
||
[问题描述](https://leetcode.cn/problems/compress-string-lcci) | ||
|
||
这个问题主要是如何遍历字符串, 找到重复的字符, 可以使用同向型双指针. | ||
然后进行字符串拼接. | ||
|
||
代码实现: | ||
|
||
## C++ | ||
|
||
```cpp | ||
{{#include main.cpp:5:39}} | ||
}; | ||
``` | ||
## Rust | ||
```rust | ||
{{#incldue src/main.rs:5:}} | ||
``` |
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,76 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
#include <cassert> | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
// 双指针法遍历字符串 | ||
static std::string compressString(std::string S) { | ||
std::string out; | ||
const int len = S.size(); | ||
|
||
// 遍历所有字符 | ||
int left = 0; | ||
while (left < len) { | ||
// 注意边界值 | ||
int right = left; | ||
while (right < len && S[left] == S[right]) { | ||
right += 1; | ||
} | ||
// 拼接字符串 | ||
out += S[left]; | ||
out += std::to_string(right - left); | ||
|
||
// 将左侧边界向右移 | ||
left = right; | ||
} | ||
// 判断压缩后的字符串是不是变短了 | ||
if (out.size() < S.size()) { | ||
return out; | ||
} else { | ||
return S; | ||
} | ||
} | ||
|
||
// 空间复杂度 O(1) | ||
static std::string compressString2(std::string S) { | ||
// 首先遍历字符串, 找出字符的最大重复次数 max_count | ||
// 如果 max_count == 2, 说明不必要压缩它, 直接返回. | ||
// | ||
// 然后对字符串进行原地压缩 | ||
// 要注意边界值 | ||
const int len = S.size(); | ||
const size_t kBufLen = 10; | ||
char buf[kBufLen]; | ||
|
||
// TODO(Shaohua): | ||
|
||
return S; | ||
} | ||
}; | ||
|
||
void checkSolution() { | ||
{ | ||
const std::string s1 = "aabcccccaaa"; | ||
const std::string expected = "a2b1c5a3"; | ||
const std::string s2 = Solution::compressString(s1); | ||
assert(s2 == expected); | ||
} | ||
|
||
{ | ||
const std::string s1 = "abbccd"; | ||
const std::string expected = "abbccd"; | ||
const std::string s2 = Solution::compressString(s1); | ||
assert(s2 == expected); | ||
} | ||
} | ||
|
||
int main() { | ||
checkSolution(); | ||
return 0; | ||
} |
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,63 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
// 双指针法 | ||
pub fn compress_string1(s: String) -> String { | ||
let mut out = String::new(); | ||
|
||
// 遍历所有字符 | ||
let mut left = 0; | ||
let bytes = s.as_bytes(); | ||
let len = bytes.len(); | ||
while left < len { | ||
// 注意边界值 | ||
let mut right = left; | ||
while right < len && bytes[left] == bytes[right] { | ||
right += 1; | ||
} | ||
out.push(char::from(bytes[left])); | ||
out.push_str(&(right - left).to_string()); | ||
|
||
// 将左侧边界向右移 | ||
left = right; | ||
} | ||
|
||
if out.len() < s.len() { | ||
out | ||
} else { | ||
s | ||
} | ||
} | ||
|
||
pub type SolutionFn = fn(String) -> String; | ||
|
||
fn check_solution(func: SolutionFn) { | ||
{ | ||
let s1 = "aabcccccaaa".to_owned(); | ||
let expected = "a2b1c5a3"; | ||
let s2 = func(s1); | ||
assert_eq!(s2, expected); | ||
} | ||
|
||
{ | ||
let s1 = "abbccd".to_owned(); | ||
let expected = "abbccd"; | ||
let s2 = func(s1); | ||
assert_eq!(s2, expected); | ||
} | ||
} | ||
|
||
fn main() { | ||
check_solution(compress_string1); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{check_solution, compress_string1}; | ||
|
||
#[test] | ||
fn test_compress_string1() { | ||
check_solution(compress_string1); | ||
} | ||
} |
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