solution.cpp 571 字节
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int numDecodings(char *s)
{
	int len = strlen(s);
	if (len == 0)
	{
		return 0;
	}
	int a = 1;
	int b = s[0] == '0' ? 0 : a;
	int c = b;
	for (int i = 2; i <= len; i++)
	{
		c = s[i - 1] == '0' ? 0 : b;
		int num = (s[i - 2] - '0') * 10 + (s[i - 1] - '0');
		if (num >= 10 && num <= 26)
		{
			c += a;
		}
		a = b;
		b = c;
	}
	return c;
}
int main(int argc, char **argv)
{
	if (argc != 2)
	{
		fprintf(stderr, "Usage: ./test number\n");
		exit(-1);
	}
	printf("%d\n", numDecodings(argv[1]));
	return 0;
}