使用两个数组分别记录字符和对应的数字,然后清除原来的vector,重新向里面添加元素。注意判断1个字符时,不将'1'加入vector。

int compress(vector<char>& chars) {
const int N = ;
char words[N];//存储字符
int count[N];//存储数字 memset(words, ' ', sizeof(words));//count初始化为全' '
memset(count, , sizeof(count));//count初始化为全0 char lastChar = ' ';//ASCII:32
int i = ;
for (auto c : chars)
{
if (c != lastChar)//新字符
{
if (lastChar == ' ')
{
i = ;
}
else
{
i++;
}
}
words[i] = c;
count[i]++;
lastChar = c;
} chars.clear();
for (int j = ; j <= i; j++)
{
chars.push_back(words[j]);
int a = count[j]; if (a == )
{
continue;
} char str[];
sprintf(str, "%d", a);
for (auto s : str)
{
if (s == '\0')
{
break;
}
chars.push_back(s);
}
}
int sum = chars.size();
return sum;
}

leetcode443的更多相关文章

  1. [Swift]LeetCode443. 压缩字符串 | String Compression

    Given an array of characters, compress it in-place. The length after compression must always be smal ...

  2. Leetcode443.String Compression压缩字符串

    给定一组字符,使用原地算法将其压缩. 压缩后的长度必须始终小于或等于原数组长度. 数组的每个元素应该是长度为1 的字符(不是 int 整数类型). 在完成原地修改输入数组后,返回数组的新长度. 进阶: ...

  3. 第六周 Leetcode 446. Arithmetic Slices II - Subsequence (HARD)

    Leetcode443 题意:给一个长度1000内的整数数列,求有多少个等差的子数列. 如 [2,4,6,8,10]有7个等差子数列. 想了一个O(n^2logn)的DP算法 DP[i][j]为 对于 ...

随机推荐

  1. C# XML对象序列化、反序列化 - PEPE YU

    http://www.tuicool.com/articles/IjE7ban http://www.cnblogs.com/johnsmith/archive/2012/12/03/2799795. ...

  2. Linux嵌入式 -- 内核 - proc文件系统

    1. 什么是proc文件系统? 实例:通过 /proc/meminfo,查询当前内存使用情况. 结论:proc文件系统是一种在用户态检查内核状态的机制. 2.Proc文件分类 特点  每个文件都规定了 ...

  3. hdoj1004--Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  4. Springboot- Caused by: org.hibernate.AnnotationException: No identifier specified for entity:

    错误与异常: Caused by: org.hibernate.AnnotationException: No identifier specified for entity: 原因:引用了不对的包, ...

  5. JavaScrip 原生多文件上传及预览 兼容多浏览器

    JavaScrip 原生多文件上传及预览 兼容多浏览器 html代码块 <div class="container"> <label>请选择一个图像文件:& ...

  6. mogon操作数据库

    返回的本来就是promise redis是内存数据库,更适合放session等一些东西.而mongo不是.

  7. java: scanner(输入流)文本扫描类

    //scanner是接受system.in输入流的操作类 //scanner同时也支持文件输入流的操作 //一个可以使用正则表达式来分析基本类型和字符串的简单文本扫描器 Scanner scan = ...

  8. sql生成excel

    gosp_configure 'show advanced options',1reconfiguregosp_configure 'xp_cmdshell',1reconfiguregoEXEC m ...

  9. 未定义的标示符“RECT”,引入了windows.h头文件也没有用?

    我用的是win8的vs2012,RECT应该引入什么头文件?windows.h我第一个就引入了,去windows.h里面搜也搜不到RECT这个关键字,应该引入哪个头文件呢? 真是奇怪啊,是不是还需要什 ...

  10. RedHat5.8 编译内核驱动 合成initrd.img

    /******************************************************************* * RedHat5.8 编译内核驱动 合成initrd.img ...