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 ...
随机推荐
- 类Shiro权限校验框架的设计和实现
前言: 之前简单集成了springmvc和shiro用于后台管理平台的权限控制, 设计思路非常的优美, 而且编程确实非常的方便和简洁. 唯一的不足, 我觉得配置稍有些繁琐. 当时我有个小想法, 觉得可 ...
- Python学习之路基础篇--01Python的基本常识
1 计算机基础 首先认识什么是CPU(Central Processing Unit),即中央处理器,相当于人类的大脑.内存,临时储存数据,断电即消失.硬盘,可以长久的储存数据,有固态硬盘,机械硬盘之 ...
- macbook hive安装
1 原材料 1.1 已经安装好的伪分布式hadoop,版本2.8.3(参见链接https://www.cnblogs.com/wooluwalker/p/9128859.html) 1.2 apach ...
- 使用SURF::create()以后报错无法解析
理论上,如果在cmake中勾选了Build_opencv_world.OPENCV_ENABLE_NONFREE以及选择了OPENCV_EXTRA_MODULES_PATH三项后,再编译INSTALL ...
- Go Example--组合函数
package main import ( "fmt" "strings" ) func Index(vs []string, t string) int { ...
- PhpAdmin支持登录远程数据库服务器
转载:http://www.cnblogs.com/andydao/p/4227312.html 该数据,百度搜不到,Google1分钟搞定 一.如何设置phpMyAdmin自动登录? 首先在根目录找 ...
- 查看mysql的版本号
查看mysql的版本号 1.1 在命令行登录mysql,即可看到mysql的版本号 [root@heyong ~]# mysql -uroot -p Enter password: Welcome t ...
- 更多FMK 的还是看万一的吧
http://www.cnblogs.com/del/category/323943.html 记录一下, 作为目录
- ubuntu-docker入门到放弃(八)创建支持SSH服务的镜像
我们知道进入docker容器可以使用attach.exec等命令来操作和管理,但是如果需要远程登录并管理容器,就需要ssh服务的支持了. 1.基于commit命令创建 docker提供了commit命 ...
- 对Array.prototype.slice.call()方法的理解
在看别人代码时,发现有这么个写法:[].slice.call(arguments, 0),这到底是什么意思呢? 1.基础 1)slice() 方法可从已有的数组中返回选定的元素. start:必需.规 ...