Skip to content

Commit

Permalink
leetcode: Add 01.06
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Sep 24, 2024
1 parent 95e0b77 commit 1bdfd56
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/leetcode/01.06.compress-string-lcci/Cargo.toml
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]
22 changes: 22 additions & 0 deletions src/leetcode/01.06.compress-string-lcci/index.md
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:}}
```
76 changes: 76 additions & 0 deletions src/leetcode/01.06.compress-string-lcci/main.cpp
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;
}
63 changes: 63 additions & 0 deletions src/leetcode/01.06.compress-string-lcci/src/main.rs
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);
}
}
2 changes: 1 addition & 1 deletion src/leetcode/tags/array/two-pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ TODO:
- [986. 区间列表的交集](https://leetcode.com/problems/interval-list-intersections)
- [1498. 满足条件的子序列数目 Number of Subsequences That Satisfy the Given Sum Condition](../../1498.number-of-subsequences-that-satisfy-the-given-sum-condition/index.md)
- [1850. Minimum Adjacent Swaps to Reach the Kth Smallest Number](https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/description/)
- [2410. Maximum Matching of Players With Trainers](https://leetcode.com/problems/maximum-matching-of-players-with-trainers/description/)
- [2410. Maximum Matching of Players With Trainers](https://leetcode.com/problems/maximum-matching-of-players-with-trainers/description/)

0 comments on commit 1bdfd56

Please sign in to comment.