题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
#include <stdio.h> #include <stdint.h> #include <string.h> //11.52-12.28 半小时,主要在调试 int main() { char str[21] = {0}; scanf("%s", str); uint8_t count[26] = {0}; memset(count, 0, sizeof(count)); //记录各个字符出现的次数 int len = strlen(str); int i; for (i = 0; i < len; i++) { count[str[i] - 'a']++; //printf("str[i]:%c\n", str[i]); } //获取最小出现的次数 uint8_t min = count[str[0] - 'a']; //printf("min:%d\n", min); for (i = 0; i < sizeof(count); i++) { if (count[i] < min && count[i] != 0) { min = count[i]; } } //printf("min:%u, count[0]:%u\n", min,count[0]); //判断是否存在多个最小出现的字母 char min_char_num = 0; char min_char_arr[20] = {0}; for (i = 0; i < sizeof(count); i++) { if (count[i] == min) { //printf("%c ", i + 'a'); min_char_arr[min_char_num++] = i + 'a'; } } char output[21] = {0}; int j = 0; for (i = 0; i < len; i++) { char skip = 0; for (int k =0; k < min_char_num; k++) { if (str[i] == min_char_arr[k]) { skip = 1; break; } } if (skip) { continue; } output[j++] = str[i]; } printf("%s", output); return 0; }
反思:
①一开始没注意到是最大还是最小,搞错了
②最小值用了 count[0],没出现也是最小啊,应用 count[str[0]-'a']才对
③固定思维,做过了最大值的,最小值的有变化,不会变通。。。