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. Linux 系统级开启文件句柄 调优

    系统级开启文件句柄  max-file系统级别的能够打开的文件句柄的数量,Centos7默认是794168. Max-file 与 ulimit -n 的区别 max-file 表示系统级别的能够打开 ...

  2. POJ 2774 Long Long Message (二分 + Hash 求最长公共子串)题解

    题意:求最长公共子串 思路:把两个串Hash,然后我们把短的作为LCS的最大可能值,然后二分长度,每次判断这样二分可不可以.判断时,先拿出第一个母串所有len长的子串,排序,然后枚举第二个母串len长 ...

  3. laravel框架——Excel导入导出

    一.composer安装PHPExcel插件 1.在框架根目录下安装依赖 composer require "maatwebsite/excel:~2.1.0" 2.打开框架在co ...

  4. IDEA中更改Tomcat服务器的URL

    先别参考以下的做法,貌似出了点状况,后续修正!!! 直接上图吧 看IDEA的右上角 点击黄色方框内的Edit Configurations,进入以下的编辑面 后面四个框内的都可以修改,当然不要修改和其 ...

  5. IDEA去除自动检测bean是否存在

    操作步骤如下图所示:

  6. C# 文件/文件夹压缩解压缩

    项目上用到的,随手做个记录,哈哈. 直接上代码: using System; using System.Data; using System.Configuration; using System.C ...

  7. 《HTTP 权威指南》笔记:第十五章 实体与编码

     如果把 「HTTP 报文」想象为因特网货运系统的「箱子」,那么「HTTP 实体」就是报文中的实际的「货物」. 其中,实体又包含了「实体首部」 和 「实体主体」,实体首部用于描述各种参数,实体主体就 ...

  8. C++ #和##运算符

    原文:https://blog.csdn.net/mitu405687908/article/details/51084441 #和##运算符 #:构串操作符 构串操作符#只能修饰带参数的宏的形参,它 ...

  9. Expression的烦恼

    var tar = Expression.Label(typeof(int)); var p1=Expression.Parameter(typeof(int), "a"); va ...

  10. java poi excel操作 下拉菜单 及数据有效性

    1 private InputStream updateTemplateStyleHSSF(InputStream inputStream,CsCustCon csCustCon) throws IO ...