LeetCode 443. String Compression (压缩字符串)
题目标签:String
这一题需要3个pointers:
anchor:标记下一个需要存入的char
read:找到下一个不同的char
write:标记需要存入的位置
让 read指针 去找到下一个char,找到后先把 anchor 位置上的 char 存入 write 位置, 然后把 char 重复的次数 转化为 char 存入下一个位置,最后重新设定 anchor = read + 1。
Java Solution:
Runtime beats 26.91%
完成日期:10/09/2018
关键词:3个指针
关键点:如果char 重复的次数大于9,需要把 int 转换为 char
class Solution
{
public int compress(char[] chars)
{
int anchor = 0;
int write = 0; for(int read = 0; read < chars.length; read++)
{
if(read + 1 == chars.length || chars[read + 1] != chars[read])
{
chars[write++] = chars[anchor]; if(read > anchor)
{
String num = read - anchor + 1 + "";
for(char c : num.toCharArray())
chars[write++] = c;
} anchor = read + 1;
}
} return write;
}
}
参考资料:https://leetcode.com/problems/string-compression/solution/
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 443. String Compression (压缩字符串)的更多相关文章
- 443 String Compression 压缩字符串
给定一组字符,使用原地算法将其压缩.压缩后的长度必须始终小于或等于原数组长度.数组的每个元素应该是长度为1 的字符(不是 int 整数类型).在完成原地修改输入数组后,返回数组的新长度.进阶:你能否仅 ...
- Leetcode443.String Compression压缩字符串
给定一组字符,使用原地算法将其压缩. 压缩后的长度必须始终小于或等于原数组长度. 数组的每个元素应该是长度为1 的字符(不是 int 整数类型). 在完成原地修改输入数组后,返回数组的新长度. 进阶: ...
- leetcode 443. String Compression
下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { ...
- 443. String Compression - LeetCode
Question 443. String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] ...
- 【leetcode】443. String Compression
problem 443. String Compression Input ["a","a","b","b"," ...
- 443. String Compression
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...
- 443. String Compression字符串压缩
[抄题]: Given an array of characters, compress it in-place. The length after compression must always b ...
- 【LeetCode】443. String Compression 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
随机推荐
- URI URL URN的区别
一:什么是URI,URL,URN ? URI:Uniform Resource Identifier,统一资源标识符,是一个用于表示互联网上资源名称的字符串 格式:http://www.xxx.com ...
- js文件中引用其他js文件
这一个功能的作用是做自己的js包时,可以通过引入一个整体的js文件而引入其他js. 只需要在总体的js加上这一句话 document.write("<script type='text ...
- delphi 字符串处理中的怪异现象与处理
1, 怪异现象:字符串相加操作不正常! 以上代码,明显输出字符串应含有后缀“.jpg”,但实际输出却不含后缀(如下),字符串加法操作似乎不起作用了! 采用showMessage进行输出,看看结果如何? ...
- 【C++】智能指针简述(三):scoped_ptr
在介绍scoped_ptr之前,我们先回顾一下前两篇文章的内容. 首先,智能指针采用RAII机制,通过对象来管理指针,构造对象时,完成资源的初始化;析构对象时,对资源进行清理及汕尾. auto_ptr ...
- Angular——作用域
基本介绍 应用App是无法嵌套的,但是controller是可以嵌套的,每个controller都会对应一个模型(model)也就是$scope对象,不同层级的controller下的$scope遍产 ...
- MFC_1.1 基本知识
如何创建一个MFC项目 选择 MFC 应用程序进行创建,不要使用非英文名 选择对话框风格进行编写 可以通过自定义的设置修改类名 MFC 的基本知识 MFC 是纯面向对象的编程,是 SDK 经过 C++ ...
- vue组件---动态组件&异步组件
(1)在动态组件上使用keep-alive 之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件.接下来简单回顾下 <component>元素是vue 里面的一个内置组件.在里面使 ...
- 事件的节流(throttle)与防抖(debounce)
事件的节流(throttle)与防抖(debounce) 有些浏览器事件可以在短时间内快速触发多次,比如调整窗口大小或向下滚动页面.例如,监听页面窗口滚动事件,并且用户持续快速地向下滚动页面,那么滚动 ...
- vue-quill-editor + element-ui upload实现富文本图片上传
代码贴上 <template> <div class="quill-editor-example"> <div class="box&quo ...
- ubuntu 14.04 挂载window共享目录
(1) 先在ubuntu系统里,新建一个目录用于挂载,目录假设为 /mnt/win: sudo mkdir /mnt/win (2)在windows系统,共享出一个文件夹,共享名称假设为www sud ...