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. Docker Swarm volume 数据持久化

    Docker Swarm volume 数据持久化 volume 是将宿主级的目录映射到容器中,以实现数据持久化. 可以用两种方式来实现: volume 默认模式:工作节点宿主机数据同步到容器内. v ...

  2. Excel提取设定的多个关键字段

    从一段文字中,提取事先设定的关键字段: 在M2单元格输入下列公式: =IFERROR(IF(FIND(O$2,Q2),O$2&" "),"")& ...

  3. CMD控制器常用命令

    dir 查看当前路径文件cd..返回上一级路径cd 转到指定的文件夹 \n 将光标移动到下一行的第一格 \t 将光标移动到下一个水平制表位置 mspaint 画图 编译源代码 javac HelloW ...

  4. 【BZOJ2301】Problem B

    Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...

  5. ComponentOne 产品经理:为什么要从C1Report迁移到FlexReport

    概述 如果你正在使用ComponentOne Enterprise 的Reports for WinForm 报表控件(C1Report),你一定会喜欢更为强大的FlexReport! FlexRep ...

  6. vivado 创建PL工程

    参考来源 https://china.xilinx.com/video/hardware/i-and-o-planning-overview.html 前言 我Win10系统上的Xilinx Plat ...

  7. 【Python】【运算符】

    [取模] 所谓取模运算,就是计算两个数相除之后的余数,符号是%.如a % b就是计算a除以b的余数.用数学语言来描述,就是如果存在整数n和m,其中0 <= m < b,使得a = n * ...

  8. spring-cloud-config——Quick Start

    参考资料: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.4.0.RELEASE/single/spring-cl ...

  9. 2018 German Collegiate Programming Contest (GCPC 18)

    2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <al ...

  10. https 适配

    1info plist <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryL ...