LWC 56:443. String Compression

LWC 56:443. String Compression

传送门:443. String Compression

Problem:

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.

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:

All characters have an ASCII value in [35, 126].

1 <= len(chars) <= 1000.

思路: 非常暴力,按顺序计数即可,遇到不同重新归零,记录上一回合信息。

代码如下:

    public int compress(char[] chars) {
        int n = chars.length;

        if (n == 0) return 0;
        StringBuilder sb = new StringBuilder();

        char p = chars[0];
        int cnt = 1;
        for (int i = 1; i < n; ++i) {
            if (chars[i] == p) {
                cnt ++;
            }
            else {
                if (cnt == 1) {
                    sb.append(p);
                }
                else {
                    sb.append(p + "" + cnt);
                }
                cnt = 1;
            }
            p = chars[i];
        }

        if (cnt == 1) {
            sb.append(p);
        }
        else {
            sb.append(p + "" + cnt);
        }

        for (int i = 0; i < sb.length(); ++i) {
            chars[i] = sb.charAt(i);
        }

        return sb.length();
    }

优化一下:

    public int compress(char[] chars) {
        int n = chars.length;

        if (n == 0) return 0;

        char p = chars[0];
        int cnt = 1;
        int tot = 0;

        for (int i = 1; i < n; ++i) {
            if (chars[i] == p) {
                cnt ++;
            }
            else {
                if (cnt == 1) {
                    chars[tot++] = p;
                }
                else {
                    chars[tot++] = p;
                    String num = String.valueOf(cnt);
                    for (int j = 0; j < num.length(); ++j) {
                        chars[tot++] = num.charAt(j);
                    }
                }
                cnt = 1;
            }
            p = chars[i];
        }

        if (cnt == 1) {
            chars[tot++] = p;
        }
        else {
            chars[tot++] = p;
            String num = String.valueOf(cnt);
            for (int j = 0; j < num.length(); ++j) {
                chars[tot++] = num.charAt(j);
            }
        }

        return tot;
    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏开发 & 算法杂谈

欢乐暑假线上编程比赛第四题:分配糖果

题目详情 有n个小朋友站成一排(编号从0到n-1),每个小朋友有一个rating值,存放在ratings数组中。老师需要给...

622
来自专栏小樱的经验随笔

Codeforces Round #411 (Div. 2)(A,B,C,D 四水题)

A. Fake NP time limit per test:1 second memory limit per test:256 megabytes inpu...

2776
来自专栏机器学习入门

LWC 63:746. Min Cost Climbing Stairs

Problem: On a staircase, the i-th step has some non-negative cost cost[i] assig...

1969
来自专栏ml

NYOJ-------三角形

Problem A 三角形 时间限制:1000 ms  |  内存限制:65535 KB 描述 在数学中,如果知道了三个点的坐标,我们就可以判断这三个点能否组成...

34113
来自专栏Petrichor的专栏

leetcode: 45. Jump Game II

1113
来自专栏用户画像

1002. A+B for Polynomials (25)

Each input file contains one test case. Each case occupies 2 lines, and each lin...

744
来自专栏King_3的技术专栏

leetcode-643-Maximum Average Subarray I

982
来自专栏拭心的安卓进阶之路

Java 解惑:Random 种子的作用、含参与不含参构造函数区别

Random 通常用来作为随机数生成器,它有两个构造方法: Random random = new Random(); Rand...

19010
来自专栏数据结构与算法

浅谈ST表

ST表 ST表的功能很简单 它是解决RMQ问题(区间最值问题)的一种强有力的工具 它可以做到O(nlogn)预处理,O(1)查询最值 算法 ST表是利用的是倍增...

2625
来自专栏mathor

给定一个数组,求子数组的最大异或和

 直接说这道题时间复杂度O(n)的做法,构建前缀树。假设将0-0、0-1、0-2、...、0-i-1的异或结果全部装在前缀树中,那么以i结尾的最大异或和就是0到...

1661

扫码关注云+社区