下面反向遍历,还是正向好。

    void left(vector<char>& v, bool p(int)) {
int max_index = v.size() - ; int del = -;
int rel = -;
while (del < max_index) {
while (p(v[del]) && del < max_index)
del++;
if (del >= max_index)
break;
if (rel < del)
rel = del;
while (!p(v[rel]) && rel <= max_index)
rel++;
if (rel > max_index)
break;
swap(v[del], v[rel]);
del++;
}
} int compress(vector<char>& chars) {
int size = chars.size();
int point = size - ;
int count = ;
for (int i = point; i >= ; i--) {
if (chars[i] == chars[i - ] && i > )
count++;
else if (count > ) {
for (int j = count - ; j > ; j--)
chars[i + j] = ;
string temp = to_string(count);
for (int j = ; j < temp.size(); j++)
chars[i + j + ] = temp[j];
count = ;
}
}
left(chars, [](int v) {return v != ;});
return count_if(chars.begin(), chars.end(), [](int v) {return v != ;});
}

其他答案:

    int compress(vector<char>& chars) {
int lo=;
int cnt=;
for(int i=; i<chars.size(); i++){
cnt++;
if(i==chars.size()-||chars[i]!=chars[i+]){
chars[lo++]=chars[i];
if(cnt>){
string nums=to_string(cnt);
for(int i=; i<nums.length(); i++){
chars[lo++]=nums[i];
}
}
cnt=;
}
}
return lo;
}

leetcode 443. String Compression的更多相关文章

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

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

  2. 【leetcode】443. String Compression

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

  3. 443. String Compression - LeetCode

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

  4. 443. String Compression

    原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...

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

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

  6. 443. String Compression字符串压缩

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

  7. [LeetCode] 443. String Compression_Easy tag:String

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

  8. 443 String Compression 压缩字符串

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

  9. [LC] 443. String Compression

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

随机推荐

  1. 自动配置IE代理脚本

    http://www.cnblogs.com/ttyp/archive/2005/11/18/279124.html

  2. mobile-net v2 学习记录。我是菜鸡!

    声明:只是自己写博客总结下,不保证正确性,我的理解很可能是错的.. 首先,mobile net V1的主要特点是: 1.深度可分离卷积.用depth-wise convolution来分层过滤特征,再 ...

  3. jquery 设计的思路-----初级

    jquery.js 很经典,其中有一些设计思路简直非常经典 1.安全的创建一个构造函数并进行调用: <script> // 这是一种安全的构造函数的创建方法,在调用构造函数G的时候,不论使 ...

  4. spring Boot 上传文件,10天后,不能上传的bug

    起因 公司研发人员 部署服务在阿里云 ecs 服务器; 上传文件过1周左右文件自动丢失; 排查思路: (1).查询tomcat 启动日志出现如下信息: java.io.IOException: The ...

  5. 用ADO操作数据库的方法步骤(ZT)

    http://www.cppblog.com/changshoumeng/articles/113437.html 学习ADO时总结的一些经验 用ADO操作数据库的方法步骤 ADO接口简介 ADO库包 ...

  6. Kotlin语言学习笔记(1)

    fun main(args: Array<String>) { println("Hello, World!") } 基本语法 声明常量用val,声明变量用var,声明 ...

  7. haproxy + nginx + proxy protocol 获得客户真实IP方法

    公司网站架构为: 前面2台HA负载均衡,后面3台Nginx负载均衡反向代理,然后后面有N台WEB服务器 由于要统计IP,需要在WEB服务器日志里体现客户端真实IP 那么问题来了,通过HA代理的HTTP ...

  8. sql help cs

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Confi ...

  9. 简单全局HOOK拦截大部分键盘消息

    前言:学习HOOK中,万一老师讲解HOOK入门教程:http://www.cnblogs.com/del/category/124150.html http://www.cnblogs.com/del ...

  10. 12.常用类简单介绍.md

    目录 1.Scanner类 2.System类 4.Object类和工具类 5.StringBuffer类和StringBuilder类 6.Math类 7.Random类和ThreadLocalRa ...