博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】443. String Compression
阅读量:6201 次
发布时间:2019-06-21

本文共 2157 字,大约阅读时间需要 7 分钟。

题目如下:

Given an array of characters, compress it .

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 , 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".

 

Example 2:

Input:["a"]Output:Return 1, and the first 1 characters of the input array should be: ["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"].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.

 

Note:

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

 

解题思路:从头到尾遍历数组,记录连续字符的个数,然后插入数组前部,注意每次插入要记录当前的偏移量offset 。

代码如下:

class Solution(object):    def compress(self, chars):        """        :type chars: List[str]        :rtype: int        """        lastChar = None        count = 0        inx = 0        offset = 0        chars.append('END') # terminator        while inx < len(chars):            i = chars[inx]            if lastChar == None:                lastChar = i                count = 1            elif lastChar == i:                count += 1            else:                lastOff = offset                chars.insert(offset,lastChar)                offset += 1                if count != 1:                    count = str(count)                    for j in count:                        chars.insert(offset, j)                        offset += 1                lastChar = i                count = 1                inx += (offset - lastOff)            inx += 1        #print chars        del chars[-1]        return offset

 

转载于:https://www.cnblogs.com/seyjs/p/9275563.html

你可能感兴趣的文章
新技术持续升级 测距传感器激起智能家居千层浪
查看>>
微信小程序与 iMessage 的 App Store,暗示了新的 App 形式吗?
查看>>
联通宣布4G在港商用 发布全球“U 计划 ”
查看>>
《Spark大数据处理:技术、应用与性能优化》——2.3 本章小结
查看>>
未来“攻击”你的可能只是一个智能门铃
查看>>
《企业大数据系统构建实战:技术、架构、实施与应用》——2.4 本章小结
查看>>
《DBA修炼之道:数据库管理员的第一本书》——3.2节数据模型的组件
查看>>
“双11”散场,电商云计算硝烟未散
查看>>
大数据时代,新闻出版业如何跟进?
查看>>
软件测试工程师的分类从新手到专家
查看>>
面部识别技术使得公众的匿名性消失
查看>>
《数据科学:R语言实现》——2.8 获取Facebook数据
查看>>
锤子手机 Bootloader 被国内越狱团队盘古破解
查看>>
《TCP/IP路由技术(第一卷)(第二版)》一1.8 总结表:第1章命令总结
查看>>
《Linux设备驱动开发详解 A》一一3.2 Linux 2.6后的内核特点
查看>>
部署 Docker 前必须问自己的四个问题
查看>>
《UG NX10中文版完全自学手册》——1.5 文件操作
查看>>
《Android 3D游戏开发技术宝典——OpenGL ES 2.0》——2.6节Socket网络通信
查看>>
比特币的交易价格节节攀升,发展势头却比以太坊弱?
查看>>
未来 5 年有颠覆性的 IT 技术都在这里
查看>>