题解 | #把数字翻译成字符串#
把数字翻译成字符串
https://www.nowcoder.com/practice/046a55e6cd274cffb88fc32dba695668
/** * 解码 * @param nums string字符串 数字串 * @return int整型 */ #include <stdio.h> #include <string.h> int get_cnt(char* s); int get_num(char* s) { return (s[0] - '0') * 10 + (s[1 ] - '0'); } int get_cnt(char* s) { int i = get_num(s); // printf("i = %d ", i); if (i == 0) { return 0xff; } if (i < 10) { return 1; } if (i == 10 || i == 20) { return 1; } if (i < 27) { return 2; } if (i % 10 == 0 ) { return 0xff; } return 1; } #define max(a,b) ((a) > (b)) ? (a) :(b) int solve(char* nums ) { // write code here int s[strlen(nums)]; s [0] = 1; if (nums[0] == '0') { return 0; } if (strlen(nums) == 1) { return 1; } int j = get_num(&nums[0]); if (j > 26 && j < 30) { s[1] = 1; } else if (j == 10 || j == 20) { s[1] = 1; } else if ( j <= 26) { s[1] = 2; } else { s[1] = 1; } if (strlen(nums) == 2) { return s[1]; } for (int i = 2; i < strlen(nums); i++) { int k = get_cnt(&nums[i - 1]); if (k == 0xff) { return 0; } if (k == 1) { s[i] = s[i - 1]; } if (k == 2) { s[i] = s[i - 2] + s[i - 1]; } if(j == 3) { s[i] = s[i - 2]; } } printf("%d", s[strlen(nums) - 1]); return s[strlen(nums) - 1]; }