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的更多相关文章
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
- 443. String Compression - LeetCode
Question 443. String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] ...
- 443. String Compression字符串压缩
[抄题]: Given an array of characters, compress it in-place. The length after compression must always b ...
- leetcode 443. String Compression
下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { ...
- LeetCode 443. String Compression (压缩字符串)
题目标签:String 这一题需要3个pointers: anchor:标记下一个需要存入的char read:找到下一个不同的char write:标记需要存入的位置 让 read指针 去找到下一个 ...
- [LC] 443. String Compression
Given an array of characters, compress it in-place. The length after compression must always be smal ...
- 【LeetCode】443. String Compression 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...
- 443 String Compression 压缩字符串
给定一组字符,使用原地算法将其压缩.压缩后的长度必须始终小于或等于原数组长度.数组的每个元素应该是长度为1 的字符(不是 int 整数类型).在完成原地修改输入数组后,返回数组的新长度.进阶:你能否仅 ...
- LeetCode_443. String Compression
443. String Compression Easy Given an array of characters, compress it in-place. The length after co ...
随机推荐
- 3.1 unittest简介
3.1 unittest简介 前言 熟悉java的应该都清楚常见的单元测试框架Junit和TestNG.python里面也有单元测试框架-unittest,相当于是一个python版的junit.py ...
- Linux学习之路——文件查找:find
使用权限:所有角色 用法:find [ options ] [ expression ]( find path -expression [ -print ] [ -exec | -ok command ...
- Oracle使用笔记(二)
一.表空间 1.创建表空间 create tablespace db_test --表空间名 datafile 'D:\oracle\product\11.2.0\dbhome_1\oradata\o ...
- CH3401 石头游戏(矩阵快速幂加速递推)
题目链接:传送门 题目: 石头游戏 0x30「数学知识」例题 描述 石头游戏在一个 n 行 m 列 (≤n,m≤) 的网格上进行,每个格子对应一种操作序列,操作序列至多有10种,分别用0~9这10个数 ...
- thinkphp 3.2.1 URL 大小写问题 下面有具体说明
问题假设方法为/Admin/User/addUser,关闭调试后,再访问时都是全部变成了小写:/admin/user/adduser1.我的APP_DEBUG是关闭的2.程序在win正常,放到linu ...
- 如何在Python中使用ZeroMQ和Docker构建微服务架构
@Container容器技术大会将于6月4日在上海光大会展中心国际大酒店举办,来自携程.PPTV.蚂蚁金服.京东.浙江移动.海尔电器.唯品会.eBay.道富银行.麻袋理财等公司的技术负责人将带来实践经 ...
- excel 表格粘贴到word 显示不完整
左上角,十字点,右键,取消固定宽度即可:
- 为git服务器配置gitosis管理权限
yum install python-setuptools git clone https://github.com/tv42/gitosis.git cd gitosis sudo python s ...
- nomad 0.9 新特性
内容摘自github Affinities and Spread: Jobs may now specify affinities towards certain node attributes. A ...
- day11 大纲
01 昨日内容回顾 函数名的运用: 1,特殊的变量. 2,函数名可以当做变量赋值. 3,函数名可以当做容器类类型的元素. 4,函数名可以当做函数的参数. 5,函数名可以当做函数的返回值. 函数的运用: ...