题目:

Given an array of characters, compress it in-place.

给定一组字符,就地压缩它。

The length after compression must always be smaller than or equal to the original array.

压缩后的长度必须始终小于或等于原始数组。

Every element of the array should be a character (not int) of length 1.

数组的每个元素都应该是长度为1的字符(不是int)。

After you are done modifying the input array in-place, return the new length of the array.

在就地修改输入数组后,返回数组的新长度。

Follow up:

跟进:
Could you solve it using only O(1) extra space?

你能用O(1)额外空间解决它吗?

Example 1:

Input:
["a","a","b","b","c","c","c"]Output:
Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]返回6,输入数组的前6个字符应为:[“a”,“2”,“b”,“2”,“c”,“3”]Explanation:
"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".“aa”被“a2”取代。 “bb”被“b2”取代。 “ccc”被“c3”取代。

Example 2:

Input:
["a"]Output:
Return 1, and the first 1 characters of the input array should be: ["a"]返回1,输入数组的前1个字符应为:[“a”]
Explanation:
Nothing is replaced.

Example 3:

Input:
["a","b","b","b","b","b","b","b","b","b","b","b","b"]Output:
Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].返回4,输入数组的前4个字符应为:[“a”,“b”,“1”,“2”]Explanation:
Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12".
Notice each digit has it's own entry in the array.由于字符“a”不重复,因此不会压缩。 “bbbbbbbbbbbbb”被“b12”取代。请注意,每个数字在数组中都有自己的条目。

Note:

  1. All characters have an ASCII value in [35, 126].
  2. 1 <= len(chars) <= 1000.

解答:

 1 class Solution {
 2     public int compress(char[] chars) {
 3         int slow=0,fast=0;
 4         while(fast<chars.length){
 5             char currChar=chars[fast];
 6             int count=0;
 7             while(fast<chars.length && chars[fast]==currChar){
 8                 fast++;
 9                 count++;
10             }
11             chars[slow++]=currChar;
12             if(count!=1)
13                 for(char c:Integer.toString(count).toCharArray())
14                     chars[slow++]=c;
15         }
16         return slow;
17     }
18 }

详解:

快慢指针 滑动窗口

转载于:https://www.cnblogs.com/chanaichao/p/9594488.html

140.String Compression相关推荐

  1. C#LeetCode刷题之#443-压缩字符串​​​​​​​(String Compression)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3943 访问. 给定一组字符,使用原地算法将其压缩. 压缩后的长度 ...

  2. 【leetcode】443. String Compression

    题目如下: Given an array of characters, compress it in-place. The length after compression must always b ...

  3. LeetCode String Compression

    原题链接在这里:https://leetcode.com/problems/string-compression/description/ 题目: Given an array of characte ...

  4. LA 3363 String Compression(dp)

    题意:给出一个字符串,再给出一个字符串压缩的方法,问该字符串最少能压缩到多少位. 思路:一直没思路啊,最后只能看题解,看了题解发现这题并没有想象中的难--用dp[i][j]表示区间i~j的最小长度,则 ...

  5. LeetCode 443 String Compression(双指针)

    问题:给出一个字符串,对其压缩.对于连续出现的字符,用字符+出现次数来表示.单个情况时,数字1可以省略. 思路:使用ancher来表示检查的起始位置,遍历时记录不相同的位置,此时作压缩替换. 具体代码 ...

  6. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  7. 案例解读:利用12c渐进式DASH分析ON CPU

    墨墨导读:本文来自墨天轮读者"Anbob"供稿,分享利用12c渐进式DASH分析"ON CPU"的过程. 墨天轮主页:https://www.modb.pro/ ...

  8. leetcode刷题规划

    LeetCode精华题目列表[刷题规划系列] – TuringPlanet 目录 算法题到底在考察什么? 题目列表 Array String Linked List Queue Stack Advan ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

最新文章

  1. php preg_match 只匹配第一个字符_PHP正则表达式核心技术完全详解 第12节知识补充 断言匹配
  2. 学长毕业日记 :本科毕业论文写成博士论文的神操作20170328
  3. 数据库中DDL,DML,DCL
  4. 正则表达式替换排除特定情况
  5. android+残留软件包名,关于使用pm hide伪卸载系统软件的一些包名
  6. opencomm在c语言中的作用,你能用C语言编写面向对象的代码吗?
  7. 基本排序(C语言版)
  8. 抓包工具Fiddler的使用说明
  9. 清理300多台MySQL数据库的过期binlog日志
  10. 计算机信息传输中枢,计算机网络综合布线及线路测试
  11. 熊猫烧香病毒样本分析
  12. 用逻辑门设计一个CPU
  13. QTTabBar 1.5.4(Beta)版本
  14. 单龙芯3A3000-7A1000PMON研究学习-(3)初步编译
  15. 计算机it是什么,IT是什么意思
  16. 射频功放学习之MATLAB绘制ADS S11参数曲线
  17. ROS基础学习之ros-tutorials(一)
  18. 数据中心云工作流任务的能效调度算法研究(1)
  19. Codeforces Round #408 (Div. 2)-C. Bank Hacking-(三种方法)分类讨论,二分,集合
  20. 字符串长度计算(包含中文)

热门文章

  1. 如何使用TCP套接字的端口来区分是哪个客户端发起的连接
  2. 通过90行代码学会HTML5 WebSQL的4种基本操作
  3. Inner Join, Left Outer Join和Association的区别
  4. win10微软图标点击无反应_win10点击任务栏没反应?这样操作就可以了
  5. c语言exit_C语言实现2048
  6. mysql drop库_Mysql 删除数据库drop database详细介绍
  7. 微软公司服务器主题软件,微软重大IT升级 七款产品巩固软件帝国
  8. python长格式_python中宽格式数据帧到长格式数据帧的转换
  9. go语言变量声明、匿名变量
  10. UE4学习-虚幻4帮助手册、通过蓝图实现动画效果、添加时间轴、添加声音、C++和蓝图事件关联