欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LeetCode 91.解码方法 Decode Ways

程序员文章站 2022-07-05 14:26:11
...

题目 https://leetcode-cn.com/problems/decode-ways/

'A' -> 1
'B' -> 2
...
'Z' -> 26

注意:
'1' -> 'A'
'01' -> 无效

给定一个只包含数字的非空字符串,请计算解码方法的总数。


const char ascii2hex[256] = 
{
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//0-9//should be static??
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//10-19
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//20-29
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//30-39
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0   ,1   ,//40-49
	2   ,3   ,4   ,5   ,6   ,7   ,8   ,9   ,0x3d,0x3d,//50-59
	0x3d,0x3d,0x3d,0x3d,0x3d,10  ,11  ,12  ,13  ,14  ,//60-69
	15  ,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//70-79
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//80-89
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,10  ,11  ,12  ,//90-99
	13  ,14  ,15  ,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//100-109
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//110-119							
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//120-129
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//130-139
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//140-149
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//150-159
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//160-169
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//170-179
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//180-189
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//190-199
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//200-209										
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//210-219
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//220-229
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//230-239
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,//240-249
	0x3d,0x3d,0x3d,0x3d,0x3d,0x3d                     //250-255
};

const char count[256] = 
{
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//0-9//should be static??
	0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,//10-19
	0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,//20-29
	0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,//30-39
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//40-49
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//50-59
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//60-69
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//70-79
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//80-89
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//90-99
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//100-109
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//110-119							
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//120-129
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//130-139
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//140-149
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//150-159
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//160-169
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//170-179
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//180-189
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//190-199
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//200-209										
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//210-219
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//220-229
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//230-239
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//240-249
	0x00,0x00,0x00,0x00,0x00,0x00                     //250-255
};

int asc2bcd(unsigned char *s,int lens,unsigned char *b)
{
	int i=0,j;
	for(i=0,j=0;i<lens;i+=2,j++)
	{
		b[j] = (unsigned char)(ascii2hex[s[i]]<<4 | ascii2hex[s[i+1]]);
	}
	return j;
}

int numDecodings(char * s){
	int len = strlen(s);
	if(len == 0)    return 0;
	int i=0;
	unsigned char flag;
	int count1=0,count2=0,count3 = 0;
	if(s[0] == '0')
		count1 = 0;
	else
		count1 = 1;
	if(len == 1)
	{
		return count1;
	}
	count2 = 0;
	asc2bcd(s,2,&flag);
	if(count[flag])
		count2 += 1;
	if(s[1] != '0')
		count2 += count1;
	if(len == 2)
	{
		return count2;
	}
	for(i=2;i<len;i++)
	{
		count3 = 0;
		if(s[i] != '0')
			count3 += count2;
		asc2bcd(&s[i-1],2,&flag);
		if(count[flag])
		{
			//printf("flag\n");
			count3 += count1;
		}
		count1 = count2;
		count2 = count3;
	}
	return count3;
}