【申明:本文仅限于自我归纳总结和相互交流。有纰漏还望各位指出。 联系邮箱:Mr_chenping@163.com】
题目:
字符串原地压缩,题目描写叙述:"eeeeeaaaff" 压缩为"e5a3f2",请编程实现
题目分析:
1、原地压缩,意思是不开辟额外的空间
2、应该考虑单个字符不压缩,比方‘q’压缩过后还是‘q’,而不应该是‘q1’
3、压缩后某个字符个数是多位数,
算法实现:
#include <stdio.h> #include <string.h> #include <stdlib.h> char *str_res(char *str) { char t; char *begin = str; char *end = str + strlen(str) - 1; while(begin < end) { t = *begin; *begin++ = *end; *end-- = t; } return str; } char *int_to_str(int num) { char *str = calloc(1, 16); char *s = str; while(num) { *str++ = num%10 + '0'; num /= 10; } *str = '