leetcode 443. 压缩字符串(String Compression)

目录

  • 题目描述:
  • 示例 1:
  • 示例 2:
  • 示例 3:
  • 进阶:
  • 解法:

题目描述:

给定一组字符,使用原地算法将其压缩。

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

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

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

示例 1:

输入:
    ["a","a","b","b","c","c","c"]

输出:
    返回6,输入数组的前6个字符应该是:["a","2","b","2","c","3"]

说明:
    "aa"被"a2"替代。"bb"被"b2"替代。"ccc"被"c3"替代。

示例 2:

输入:
    ["a"]

输出:
    返回1,输入数组的前1个字符应该是:["a"]

说明:
    没有任何字符串被替代。

示例 3:

输入:
    ["a","b","b","b","b","b","b","b","b","b","b","b","b"]

输出:
    返回4,输入数组的前4个字符应该是:["a","b","1","2"]。

说明:
    由于字符"a"不重复,所以不会被压缩。"bbbbbbbbbbbb"被“b12”替代。
    注意每个数字在数组中都有它自己的位置。

进阶:

你能否仅使用O(1) 空间解决问题?

注意:

  1. 所有字符都有一个ASCII值在[35, 126]区间内。
  2. 1 <= len(chars) <= 1000

解法:

class Solution {
public:
    string toString(int num){
        string res = "";
        if(num == 0){
            return "0";
        }else{
            while(num != 0){
                res = char(num%10 + '0') + res;
                num /= 10;
            }
            return res;
        }
    }

    int compress(vector<char>& chars) {
        int sz = chars.size();

        int i = 0, j = 0, idx = 0;
        while(i < sz){
            while(j < sz && chars[j] == chars[i]){
                j++;
            }
            int num = j - i;
            chars[idx++] = chars[i];
            if(num != 1){
                string _num = toString(num);
                for(char ch : _num){
                    chars[idx++] = ch;
                }
            }
            i = j;
        }
        return idx;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10592123.html

时间: 2024-10-21 08:07:03

leetcode 443. 压缩字符串(String Compression)的相关文章

[LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器

Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext. The given compressed string will be in the form of each letter followed by a positive integer representing the numbe

443. String Compression - LeetCode

Question 443.?String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] chars) { if (chars.length == 0) return 0; StringBuilder sb = new StringBuilder(); char cur = chars[0]; int sum = 1; for (int i = 1; i <= chars.length

codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: dp[i]表示前i个字符需要的最小次数. dp[i] = min(dp[j]+w(j+1,i)); (0<=j<i); [j+1,i]如果存在循环节(自身不算),那么取最小的循环节x.w = digit((i-j)/x)+x; 否则w = i-j+1; 求一个区间最小循环节: 证明:http://w

[CareerCup] 1.5 Compress String 压缩字符串

1.5 Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your met

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

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

Leetcode 344:Reverse String 反转字符串(python、java)

Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place wit

LeetCode | 1374. Generate a String With Characters That Have Odd Counts生成每种字符都是奇数个的字符串【Python】

LeetCode 1374. Generate a String With Characters That Have Odd Counts生成每种字符都是奇数个的字符串[Easy][Python][字符串] Problem LeetCode Given an integer n, return a string with n characters such that each character in such string occurs an odd number of times. The

Educational Codeforces Round 25 F. String Compression(kmp+dp)

题目链接:Educational Codeforces Round 25 F. String Compression 题意: 给你一个字符串,让你压缩,问压缩后最小的长度是多少. 压缩的形式为x(...)x(...)  x表示(...)这个出现的次数. 题解: 考虑dp[i]表示前i个字符压缩后的最小长度. 转移方程解释看代码,这里要用到kmp来找最小的循环节. 当然还有一种找循环节的方式就是预处理lcp,然后通过枚举循环节的方式. 这里我用的kmp找的循环节.复杂度严格n2. 1 #inclu

区间DP UVA 1351 String Compression

题目传送门 1 /* 2 题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度 3 区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = min (dp[j][k] + dp[k+1][i+j-1]), 4 digit (i / k) + dp[j][j+k-1] + 2)后者表示可以压缩成k长度连续相同的字符串 4.5 详细解释 5 */ 6 /*****************************************