原题:

443. String Compression

解题:

看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并未修改原始vector的元素。

代码如下:

class Solution {
public:
int bitsOfNumber(int n)
{
int cnt = 0;
if (n <= 1) return 0; //若为1,则不增加
while(n) //统计位数,如12就是2位
{
cnt++;
n /=10;
}
return cnt;
}
int compress(vector<char>& chars)
{
int size = chars.size();
int i = 0;
int sum = 0;
map <char, int> mapchar;
map <char, int>::iterator it;
for(;i < size;i++)
{
mapchar[chars[i]]++; //统计各个字符个数
}
it = mapchar.begin();
while(it != mapchar.end())
{
if(it->second != 0) //如果有这个字符
{
sum += bitsOfNumber(it->second) + 1; //字符本身也占据一个元素位置,所以要加1
}
it++;
}
return sum;
}
};

  AC代码如下:

class Solution {
public:
int compress(vector<char>& chars) {
int len = chars.size() ;
if (len < 2)
return len ;
int res = 0 ;
char c = chars[0] ;
int num = 1 ;
chars.push_back(' ') ;
for(int i = 1 ; i < len+1 ; i++){
if (chars[i] == chars[i-1])
num++ ;
if (chars[i] != chars[i-1] ){
chars[res++] = c ;
if (num > 1){
string s = to_string(num) ;
for(int j = 0 ; j < s.size() ; j++){
chars[res++] = s[j] ;
}
}
num = 1 ;
c = chars[i] ;
}
}
return res ;
}
};

  

443. String Compression的更多相关文章

  1. 【leetcode】443. String Compression

    problem 443. String Compression Input ["a","a","b","b"," ...

  2. 443. String Compression - LeetCode

    Question 443. String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] ...

  3. 443. String Compression字符串压缩

    [抄题]: Given an array of characters, compress it in-place. The length after compression must always b ...

  4. leetcode 443. String Compression

    下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { ...

  5. LeetCode 443. String Compression (压缩字符串)

    题目标签:String 这一题需要3个pointers: anchor:标记下一个需要存入的char read:找到下一个不同的char write:标记需要存入的位置 让 read指针 去找到下一个 ...

  6. [LC] 443. String Compression

    Given an array of characters, compress it in-place. The length after compression must always be smal ...

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

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

  8. 443 String Compression 压缩字符串

    给定一组字符,使用原地算法将其压缩.压缩后的长度必须始终小于或等于原数组长度.数组的每个元素应该是长度为1 的字符(不是 int 整数类型).在完成原地修改输入数组后,返回数组的新长度.进阶:你能否仅 ...

  9. LeetCode_443. String Compression

    443. String Compression Easy Given an array of characters, compress it in-place. The length after co ...

随机推荐

  1. Windows10 VS2017 C++ Server Socket简单服务器端与客户端

    服务端: #include "pch.h" #include<iostream> #include<WinSock2.h> #include <Ws2 ...

  2. ArcGIS统计栅格像元值并转换为矢量图层

    很多时候,我们需要得到矢量数据区域所对应栅格数据的像元统计值(求平均.求和等),然后将获得的统计值赋给矢量图层的属性表,在ArcGIS中操作如下:(PS:第一次写技术文章,望大家多多体谅与支持,么么哒 ...

  3. Unable to resolve module crypto

    Add rn-nodeify to your devDependencies in package.json: "devDependencies": { "rn-node ...

  4. BZOJ1800:fly 飞行棋 (双指针 组合数)

    pro: 给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依圆周顺序排列. 请找出这些点中有没有可以围成矩形的,并希望在最短时间内找出所有不重复矩形. N<20; sol:很可能被 ...

  5. Python全栈之路----常用模块----datetime模块详解

    相比于time模块,datetime模块的接口则更直观,更容易调用. datetime模块定义了下面这几个类: datetime.date:表示日期的类,常用的属性有year,month,day: d ...

  6. 通过父元素的hover控制子元素的显示

    .searbar_content_box:hover .searchBar_checked_detail_box{ display:block}

  7. 使用maven profile指定配置文件打包适用多环境

    新建maven项目,   在pom.xml中添加 profile节点信息如下: <profiles> <profile> <!-- 开发环境 --> <id& ...

  8. 开源WHMCS支付宝当面付和即时到账插件

    开源WHMCS支付宝当面付和即时到账插件 链接: https://pan.baidu.com/s/1i5HU4hn 密码: crq7

  9. HTTP进阶学习笔记

    代理 HTTP的代理服务器既是Web服务器,又是Web客户端.使用代理可以"接触"到所有流过的HTTP流量,代理可以对其进行监视和修改.常见的就是对儿童过滤一些"成人&q ...

  10. py-day2-3 python 字典

    # 字典的value可以是任何值 test = {'k1':'v1', 'k2':True, 'k3':[1,2,3,4,{'t1':'v2','t2':'v3'},5,6], 'k4':(88,99 ...