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 ...
随机推荐
- day15-函数进阶
1.函数嵌套 多个函数嵌套在一起即为函数嵌套 在调用函数时,函数需在调用之前定义,如果函数在调用之后才定义,则不能被成功调用.当定义多个函数时,函数名称不能相同,否则后定义的函数会将之前的函数覆盖,即 ...
- git学习入门
git: 安装 git是目前最流行的版本管理系统,分为github(公共开源,代码可随意下载)和gitlib(私有化,企业使用).
- Zookeeper 3、Zookeeper工作原理(转)
1.Zookeeper的角色 » 领导者(leader),负责进行投票的发起和决议,更新系统状态 » 学习者(learner),包括跟随者(follower)和观察者(observer),follow ...
- C++17尝鲜:编译期 if 语句
Constexpr If(编译期 if 语句) 以 if constexpr 打头的 if 语句被称为 Constexpr If. Constexpr If 是C++17所引入的新的语法特性.它为C+ ...
- 如何配置windows定时任务
Windows上配置任务定时执行有两种方法.一是通过控制面板中的界面配置,另外一种是通过schtasks命令配置.如果是简单的定时任务配置(比如每天单次执行)建议选择界面的方式,简洁.直观.易上手.如 ...
- [CI]CodeIgniter特性 & 结构
------------------------------------------------------------------------------------------------- 市场 ...
- Delphi XE3通过ADOConnection 连接 MySQL 5.5.27 数据库
Delphi XE3通过ADOConnection 连接 MySQL 5.5.27 数据库 unit Unit1; interface uses Winapi.Windows, Winapi.Mess ...
- delphi 大文件的读写 使用 MapviewOffile
unit filemap; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- 19.Observales
然后 ng serve看看能不能启动 OK
- JAVA程序员常用英语
JAVA程序员常用英语 干程序员这行实在是离不开英语,干程序员是一项很辛苦的工作,要成为一个高水平的程序员尤为艰难.这是因为计算机软件技术更新的速度越来越快,而这些技术大多来源于英语国家,我们在引进这 ...