Run-length encoding · 游程编码
Run-length encoding
Compress a string by replacing each run of the same character with that character followed by how many times it repeats: aaabbc → a3b2c1. Always write the count, even when it is 1.
Walk the string tracking the current character and how long its run is. When the next character differs, write out the run (character + count) and start a fresh run. Don't forget to write the last run after the loop ends.
游程编码
通过把每段连续相同的字符(游程)替换为「该字符 + 它重复的次数」来压缩字符串:aaabbc → a3b2c1。次数始终要写出,即使是 1。
遍历字符串,记录当前字符以及它这段游程的长度。当下一个字符不同时,写出这段游程(字符 + 次数)并开始新的一段。别忘了在循环结束后写出最后一段游程。
Write run_length_encode(s) that compresses runs of the same character into the character followed by its run length, e.g. aaabbc becomes a3b2c1. Always include the count, even when it is 1. · 编写 run_length_encode(s),把连续相同的字符压缩成「字符 + 出现次数」,例如 aaabbc 变成 a3b2c1。次数始终要写出,即使是 1。
Click Run to see the output here. · 点击“运行”查看此处输出。