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 ...
随机推荐
- (转载)Android下Affinities和Task
源文链接:http://appmem.com/archives/405 1.Activity和Task task就好像是能包含很多activity的栈. 默认情况下,一个activity启动另外一个a ...
- 模板引擎Dot
Dot.js 很轻,处理速度也快,作为将json数据赋值到html页面的最好帮手. html5新引入的<template></template>就不用原先的<script ...
- jquery循环方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- React Native,flexbox布局
Flexbox布局 flex:使组件在可利用的空间内动态地扩张或收缩.flex:1会使组件撑满空间.当有多个组件都指定了flex的值,那么谁的flex值大谁占得空间就大,占得大小的比例就是flex值的 ...
- 尚硅谷springboot学习19-日志切换
查看相关依赖关系,排除相关依赖,引入新的日志依赖 slf4j+log4j的方式: <dependency> <groupId>org.springframework.boot& ...
- LeetCode OJ 89. Gray Code
题目 The gray code is a binary numeral system where two successive values differ in only one bit. Give ...
- plsql和tsql常用函数比对
http://www.jb51.net/list/list_154_1.htm 数学函数 1.绝对值 S:select abs(-1) value O:select abs(-1) value fro ...
- mysql 远程 ip访问
默认情况下Linux内的mysql数据库mysql,user表内的用户权限只是对localhost即本机才能登陆.需要更改权限: 如下的方式确认: root#mysql -h localhost-u ...
- webpack 中使用 vue-router 注意
//render 会把el指定的容器中所有的内容都清空把#app也会去掉 都在c(app)其中的app组件中展示 所有router-link router-view要写在app这个组件里面 //A ...
- jenkins commande not found
解决方法: http://www.geekcome.com/content-10-3868-1.html 1.控制台执行 echo $PATH 把输出的这句话复制 2.jenkins->系统管理 ...