Skip to content

Run-length encoding · ⁨游程编码⁩

English

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。

遍历字符串,记录当前字符以及它这段游程的长度。当下一个字符不同时,写出这段游程(字符 + 次数)并开始新的一段。别忘了在循环结束后写出最后一段游程。

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩