leetcode 443. String Compression
下面反向遍历,还是正向好。
void left(vector<char>& v, bool p(int)) {
int max_index = v.size() - ;
int del = -;
int rel = -;
while (del < max_index) {
while (p(v[del]) && del < max_index)
del++;
if (del >= max_index)
break;
if (rel < del)
rel = del;
while (!p(v[rel]) && rel <= max_index)
rel++;
if (rel > max_index)
break;
swap(v[del], v[rel]);
del++;
}
}
int compress(vector<char>& chars) {
int size = chars.size();
int point = size - ;
int count = ;
for (int i = point; i >= ; i--) {
if (chars[i] == chars[i - ] && i > )
count++;
else if (count > ) {
for (int j = count - ; j > ; j--)
chars[i + j] = ;
string temp = to_string(count);
for (int j = ; j < temp.size(); j++)
chars[i + j + ] = temp[j];
count = ;
}
}
left(chars, [](int v) {return v != ;});
return count_if(chars.begin(), chars.end(), [](int v) {return v != ;});
}
其他答案:
int compress(vector<char>& chars) {
int lo=;
int cnt=;
for(int i=; i<chars.size(); i++){
cnt++;
if(i==chars.size()-||chars[i]!=chars[i+]){
chars[lo++]=chars[i];
if(cnt>){
string nums=to_string(cnt);
for(int i=; i<nums.length(); i++){
chars[lo++]=nums[i];
}
}
cnt=;
}
}
return lo;
}
leetcode 443. String Compression的更多相关文章
- LeetCode 443. String Compression (压缩字符串)
题目标签:String 这一题需要3个pointers: anchor:标记下一个需要存入的char read:找到下一个不同的char write:标记需要存入的位置 让 read指针 去找到下一个 ...
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
- 443. String Compression - LeetCode
Question 443. String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] ...
- 443. String Compression
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...
- 【LeetCode】443. String Compression 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...
- 443. String Compression字符串压缩
[抄题]: Given an array of characters, compress it in-place. The length after compression must always b ...
- [LeetCode] 443. String Compression_Easy tag:String
Given an array of characters, compress it in-place. The length after compression must always be smal ...
- 443 String Compression 压缩字符串
给定一组字符,使用原地算法将其压缩.压缩后的长度必须始终小于或等于原数组长度.数组的每个元素应该是长度为1 的字符(不是 int 整数类型).在完成原地修改输入数组后,返回数组的新长度.进阶:你能否仅 ...
- [LC] 443. String Compression
Given an array of characters, compress it in-place. The length after compression must always be smal ...
随机推荐
- requirements.txt 的使用与创建
1. requirements.txt 主要是记录你的python 解释器安装了那些第三方模块,这样好方便项目迁移,自动解决掉项目的依赖关系 2. 网上找的那些关于 requirements 的文档 ...
- .net DLL 注册 regasm delphi调用
.net DLL 注册 regasm regasm regasm myTest.dll regasm.exe 打开vs2005自带的工具“Visual Studio 2005命令提示”,输入上述命令 ...
- HTML 圆心节点
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- maven使用fingbugs插件
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>findbugs-maven-plu ...
- Tomcat命令
如果原始内存不够用经常内存溢出,可以在catalina.bat中设置: 电脑2G内存的情况 :set JAVA_OPTS='-server -Xms1024m -Xmx1536m -XX:PermSi ...
- UI5-学习篇-4-SCP-SAP WEB IDE登录
1.注册SAP账号 登录SAP官网:https://www.sap.com/index.html 注册Register 填完相关信息,勾选条款,然后提交. 账号激活:完成后需到Email邮件中激活链接 ...
- Java ssl认证记录
听到有人在用,所以自己随便搜了搜试了下,这里就是简单记录 就是操作了一遍这篇博文 https://blog.csdn.net/a495614205/article/details/12648939 i ...
- log4j日志输出级别变更
1. 现阶段log4j日志输出配置 示例:基础服务日志配置 #DEBUG < INFO < WARN < ERROR < FATAL\u65E5\u5FD7\u7684\u ...
- PRC远程过程调用
RPC(Remote Promote Call) 一种进程间通信方式.允许像调用本地服务一样调用远程服务. RPC框架的主要目标就是让远程服务调用更简单.透明.RPC框架负责屏蔽底层的传输方式(TCP ...
- How to Pronounce TH after N or Z
How to Pronounce TH after N or Z Share Tweet Share Tagged With: Linking Consonant to Consonant The T ...