leetcode443
2024-10-21 05:51:06
使用两个数组分别记录字符和对应的数字,然后清除原来的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;
}
最新文章
- spark standalone ha spark submit
- About SSDT BI
- Swift安装
- [Java基础] SequenceInputStream输入合并流
- GL_Oracle Erp月结和年节流程讨论(概念)
- 将Magento后台汉化的方法
- BZOJ_1612_[Usaco2008_Jan]_Cow_Contest_奶牛的比赛_(dfs)
- 用 ISNULL(), NVL(), IFNULL() and COALESCE() 函数替换空值
- [HttpException (0x80004005): Failed to Execute URL.]之画蛇添足之痛
- Jquery多级菜单插件Slimmenu使用说明
- Java开发中的23+2种设计模式学习个人笔记(未完待续)
- 简洁灵活的前端框架------BootStrap
- eclipse复制粘贴变卡的解决办法
- python之路5-函数
- Linux:Debian系统的安装
- 阿里云IoT
- Netty 系列三(ByteBuf).
- maven 将jar包推送到自己本机的maven库
- Linux下编辑、编译、调试命令总结——gcc和gdb描述
- JAVA设计模式详解(六)----------状态模式
热门文章
- hibernate.cfg.xml_属性";connection.url";_指定字符集
- 双十字路口交通仿真程序(VS2010+MFC)
- jsp用tags传递参数
- Python爬虫之利用BeautifulSoup爬取豆瓣小说(三)——将小说信息写入文件
- hdu 1907 John(anti nim)
- hdoj-1106-排序(stringstream)
- python导入图片
- Unity3D教程:制作与载入AssetBundle
- mysql前缀索引的应用
- PageRank算法原理及实现