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.

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?

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"] Explanation:
"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".
class Solution {
public int compress(char[] chars) {
if (chars.length <= 1) {
return chars.length;
}
int index = 0;
int res = 0;
while (index < chars.length) {
int curCount = 0;
char curChar = chars[index];
while (index < chars.length && chars[index] == curChar) {
index += 1;
curCount += 1;
}
chars[res++] = curChar;
if (curCount != 1) {
for (char c: Integer.toString(curCount).toCharArray()) {
chars[res] = c;
res += 1;
}
}
}
return res;
}
}
												

[LC] 443. String Compression的更多相关文章

  1. 【leetcode】443. String Compression

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

  2. 443. String Compression

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

  3. 443. String Compression - LeetCode

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

  4. 443. String Compression字符串压缩

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

  5. leetcode 443. String Compression

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

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

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

  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. VS2017中使用ReportViewer控件,vs2017找不到Microsoft Rdlc Report Designer for Visual Studio

    VS2017中没有ReportViewer控件,这个控件用来实现在项目中显示和打印关系数据库中的表比较容易,特别是想要打印的时候,这个比用DataGridView和PrintDocument要简单一些 ...

  2. 用户体验评价——win10自带微软拼音输入法

    目前正在使用的一款输入法就是win10自带的微软拼音输入法, 用户界面,一直在用个人感觉最大的特点就是十分简洁,界面的布局十分清晰,可以隐藏悬浮窗让他显示在菜单栏, 另外其记住用户输入习惯方面也十分优 ...

  3. Vue.js 之 过渡动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 二十三种设计模式 python实现

    设计模式是什么? 设计模式是经过总结.优化的,对我们经常会碰到的一些编程问题的可重用解决方案.一个设计模式并不像一个类或一个库那样能够直接作用于我们的代码.反之,设计模式更为高级,它是一种必须在特定情 ...

  5. Java并发分析—synchronized

    在计算机操作系统中,并发在宏观上是指在同一时间段内,同时有多道程序在运行. 一个程序可以对应一个进程或多个进程,进程有独立的存储空间.一个进程包含一个或多个线程.线程堆空间是共享的,栈空间是私有的.同 ...

  6. Vue.js——1.初识Vue

    初识Vue.js 1. 什么是Vue.js Vue.js是前端主流框架之一,现在看招聘几乎都要求会vue 好像成了前端的代名词. Vue.js是构建界面 只关注视图层V,也就是页面的, 2. 为什么要 ...

  7. LNMP安装问题

    查什么占用了端口   netstat -nlp |grep :80 root@zzx:/usr/local/mysql# netstat -nlp |grep :80tcp        0      ...

  8. Java并发编程:CountDownLatch、CyclicBarrier和 Semaphore , Condition

    http://www.importnew.com/21889.html 1)CountDownLatch和CyclicBarrier都能够实现线程之间的等待,只不过它们侧重点不同: CountDown ...

  9. Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path). BitcoinJSONRPCClient异常、及其他异常

    1.异常信息 Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path) ...

  10. win10系统开发环境安装studio 3T(MongoDB桌面客户端)

    studio 3T 是mongodb优秀的桌面客户端工具. 下载 https://studio3t.com/download/#windows 本教程基于2020.1.2版本 安装 F:\javawe ...