cracking the coding interview No1.5
2014-10-19 16:21
399 查看
1.5 Implement a method to perform basic string compression using the counts of repeated characters.For example.
For example,the string aabcccccaaa would become a2b1c5a3.If the “compressed”string would not become smaller
than the original string,your method should return the original string.
For example,the string aabcccccaaa would become a2b1c5a3.If the “compressed”string would not become smaller
than the original string,your method should return the original string.
void change(char *str) { if (str == NULL) return; int count = 0,length = strlen(str); int k = 0; char c = str[0]; char *laststring = new char[length+1]; for (int i = 0;i < length;i++) { if (c == str[i]) { count++; } else { laststring[k++] = c; laststring[k++] = count+'0'; c = str[i]; count = 0; } } laststring[k] = '\0'; if (k > length) { printf("%s\n",str); } else { printf("%s\n",laststring); } }
相关文章推荐
- cracking the coding interview No1.4
- Cracking The Coding Interview 1.5
- cracking the coding interview No1.1
- cracking the coding interview No1.6
- cracking the coding interview No1.2
- cracking the coding interview No1.7
- cracking the coding interview No1.8
- Cracking the Coding Interview Q1.5
- cracking the coding interview problem solution 1.5
- cracking the coding interview No1.3
- Cracking The Coding Interview 3rd -- 1.5*
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目6
- 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目6
- 《Cracking the Coding Interview》——第13章:C和C++——题目2
- Cracking the coding interview--Q20.10
- 《Cracking the Coding Interview》——第14章:Java——题目1
- Cracking the coding interview--Q1.6
- 《Cracking the Coding Interview》——第16章:线程与锁——题目1
- Cracking The Coding Interview 9.7
- 《Cracking the Coding Interview》——第18章:难题——题目3