problem

443. String Compression

Input
["a","a","b","b","c","c","c"]
Output
["a","a","b","b","c","c"]
Expected
["a","","b","","c",""]

a:

Given an array of characters, compress it in-place.
After you are done modifying the input array in-place, return the new length of the array.

ss

Input
["a","a","a","b","b","a","a"]
Output
["a","","b",""]
Expected
["a","","b","","a",""]

ss

solution1:

class Solution {
public:
int compress(vector<char>& chars) {
if(chars.empty()) return ;
string res="";
char cur, pre;
int num = ;
for(int i=; i<chars.size(); i++)
{
cur = chars[i];
if(i==)
{
res += cur;
pre = cur;
} if(cur!=pre)
{
if(num>) res += to_string(num);//
res += cur;
pre = cur;
num = ;
}
else num++;
}
if(num>) res += to_string(num);
//
int len = ;
if(res.size()> chars.size()) len = chars.size();
else if(res.size()<=chars.size())
{
len = res.size();
for(int i=;i<res.size(); i++)
{
chars[i] = res[i];
} }
return len; }
};

参考

1. Leetcode_443. String Compression;

【leetcode】443. String Compression的更多相关文章

  1. 【LeetCode】443. String Compression 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...

  2. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  3. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  4. 【LeetCode】984. String Without AAA or BBB 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...

  5. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  6. 【leetcode】 Interleaving String (hard)

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  7. 【leetcode】 Scramble String (hard)★

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  8. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  9. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

随机推荐

  1. CSS 页面布局、后台管理示例

    CSS 页面布局.后台管理示例 页面布局 1.头部菜单 2.中间内容/中间左侧菜单 3.底部内容 <div class='pg-header'> <div style='width: ...

  2. 再谈git和github-深入理解-3

    git tag -a 和 -m的区别? -a是 注解 是单词 "annotate"的意思 , 表示 "给标签一个名字, 标签名 -m 是创建标签时的消息备注 git ta ...

  3. 深入分析Parquet列式存储格式【转】

    Parquet是面向分析型业务的列式存储格式,由Twitter和Cloudera合作开发,2015年5月从Apache的孵化器里毕业成为Apache顶级项目,最新的版本是1.8.0. 列式存储 列式存 ...

  4. html css js 细节

    细节1 1.Chrome中文界面下会将小于12px的字体默认显示为12px,解决方法:在CSS中加入-webkit-text-size-adjust:none; 2.link可以加载除CSS以外的其他 ...

  5. Eclipse的Outline功能栏调出来

    window-->Shoe View -->Outline 按住鼠标左键拖一下Outline功能栏,就可以无论开哪个项目都能够在右边显示Outline功能栏 转载地址:https://bl ...

  6. Asp.net core 学习笔记 (library)

    refer : https://docs.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio https://do ...

  7. Dynamic networks | 动态网络

    Dynamic networks reveal key players in aging 系统生物学中的网络分析 网络的拓扑结构:topological properties, 网络的度:whole ...

  8. arch Linux(二)

    配置你的基本系统 下列是基于该视频4:40s的流水- 切换到普通用户: [root@eric-laptop ~]# su eric 查看系统信息: [eric@eric-laptop root]$ n ...

  9. 覃超:Facebook的项目开发流程和工程师的绩效管理机制

    覃超:Facebook的项目开发流程和工程师的绩效管理机制 http://mp.weixin.qq.com/s?__biz=MjM5MDE0Mjc4MA==&mid=2650992350&am ...

  10. python四

    三元运算 name = "张三" if 1 == 2 else "李四" print(name) name1 = "张三" if 1 == ...