使用两个数组分别记录字符和对应的数字,然后清除原来的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;
}

最新文章

  1. spark standalone ha spark submit
  2. About SSDT BI
  3. Swift安装
  4. [Java基础] SequenceInputStream输入合并流
  5. GL_Oracle Erp月结和年节流程讨论(概念)
  6. 将Magento后台汉化的方法
  7. BZOJ_1612_[Usaco2008_Jan]_Cow_Contest_奶牛的比赛_(dfs)
  8. 用 ISNULL(), NVL(), IFNULL() and COALESCE() 函数替换空值
  9. [HttpException (0x80004005): Failed to Execute URL.]之画蛇添足之痛
  10. Jquery多级菜单插件Slimmenu使用说明
  11. Java开发中的23+2种设计模式学习个人笔记(未完待续)
  12. 简洁灵活的前端框架------BootStrap
  13. eclipse复制粘贴变卡的解决办法
  14. python之路5-函数
  15. Linux:Debian系统的安装
  16. 阿里云IoT
  17. Netty 系列三(ByteBuf).
  18. maven 将jar包推送到自己本机的maven库
  19. Linux下编辑、编译、调试命令总结——gcc和gdb描述
  20. JAVA设计模式详解(六)----------状态模式

热门文章

  1. hibernate.cfg.xml_属性&quot;connection.url&quot;_指定字符集
  2. 双十字路口交通仿真程序(VS2010+MFC)
  3. jsp用tags传递参数
  4. Python爬虫之利用BeautifulSoup爬取豆瓣小说(三)——将小说信息写入文件
  5. hdu 1907 John(anti nim)
  6. hdoj-1106-排序(stringstream)
  7. python导入图片
  8. Unity3D教程:制作与载入AssetBundle
  9. mysql前缀索引的应用
  10. PageRank算法原理及实现