当前位置:网站首页>Leetcode:443 Compressed string
Leetcode:443 Compressed string
2022-04-21 23:26:00 【Oceanstar's study notes】
Title source
title


title
Double pointer
In order to achieve in-situ compression , We can use double pointers to identify where we read and write in the string . Every time you read the pointer read Move to the rightmost side of a continuous substring , We're just writing pointers write Write the character and the length of the substring corresponding to the substring in turn .
- When reading pointer read At the end of the string , perhaps read When the character pointed to is different from the next character , We think it's time to read Located on the rightmost side of a continuous and identical substring . The pointer of the character string is the corresponding character string read The string pointed to . We use variables left Record the leftmost position of the substring , In this way, the length of the substring is read−left+1.
- Be careful , In order to achieve O(1) Spatial complexity , We need to realize the function of converting numbers into strings and writing them into the original strings . Here, we use the short division method to write the length of the substring in reverse order into the original string , Then reverse it .
class Solution {
public:
int compress(vector<char>& chars) {
int len = chars.size();
int write = 0, left = 0;
for (int read = 0; read < len; ++read) {
if(read == len - 1 || chars[read] != chars[read + 1]){
chars[write++] = chars[read];
int num = read - left + 1;
if(num > 1){
int anchor = write;
while (num > 0){
chars[write++] = num % 10 + '0';
num /= 10;
}
std::reverse(&chars[anchor], &chars[write]);
}
left = read + 1;
}
}
return write;
}
};


class Solution {
public:
int compress(vector<char>& chars) {
int len = chars.size(), curr = 0;
for (int i = 0, j = 0; i < len; i = j) {
while (j < len && chars[j] == chars[i]){
++j;
}
chars[curr++] = chars[i];
if(j - i == 1){
continue;
}
for(char c : std::to_string(j - i)){
chars[curr++] = c;
}
}
return curr;
}
};
版权声明
本文为[Oceanstar's study notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212324225256.html
边栏推荐
- Qt自定义控件01 简易计时器
- MySQL problem solving_ Multi table joint query_ The operator wants to calculate the average answer volume of users from different schools and different difficulties who participated in the answer. Ple
- golang力扣leetcode 2246.相邻字符不同的最长路径
- 自定義登錄成功處理
- Swoole high performance in memory database use and configuration tutorial
- Following Huawei Cangjie, it reproduces four domestic programming languages in various forms, including one 0 code
- 【acwing】1125. Cattle travel * * * (Floyd)
- Uni app image adaptation dynamic calculation of image height
- 点滴浓缩洁净,洗衣液行业的破局之路
- BUUCTF 刷题记录
猜你喜欢

2022r2 mobile pressure vessel filling test exercises and online simulation test

How to build an observable system that can "evolve continuously"| QCon
Technology, products and brand are not problems. For SAIC Audi, these two points may be life and death

leetcode:440. 字典序的第K小数字

【acwing】1125. 牛的旅行***(floyd)

There are Chinese characters in the input parameter, and an error of 500 is reported. There is an internal error in the server

大厂面试必备技能,android音视频框架

C language: simple profit and bonus

Buuctf question brushing record

Textview tilt properties
随机推荐
通过点击导入的文件或点击组件进入对应的组件页面进行编辑
. 101 keyboard events
Exception handler
leetcode:440. 字典序的第K小数字
[H.264] simple encoder and SPS
从零开始自制实现WebServer(十六)---- 学习新工具CMake自动编写MakeFile 分门别类整理源文件心情愉悦
prompt 你到底行不行?
云原生架构下的微服务选型和演进
(7) Ruixin micro rk3568 builderoot adds compiled scripts and binary program files
Ruffian Heng embedded: talk about the application and influence of system watchdog wdog1 in the startup of i.mxrt1xxx system
New independent version of the tiktok machine full repair version with video tutorial
GIC spec之ITS和LPI中断5
长沙好人
There are Chinese characters in the input parameter, and an error of 500 is reported. There is an internal error in the server
文件操作和IO
Necessary skills for large factory interview, Android audio and video framework
339-Leetcode 单词规律
Why don't MySQL use select * as query criteria? (continuously updated)
. 100 roller events
Ijcai2022 employment results released! The acceptance rate is 15%. Did you win?