剑指Offer(牛客版)--面试题46:把数字翻译成字符串

问题描述:

给定一个数字,我们按照如下规则把它翻译为字符串:0翻译成”a”,1翻译成”b”,……,11翻译成”l”,……,25翻译成”z”。一个数字可能有多个翻译。例如12258有5种不同的翻译,它们分别是”bccfi”、”bwfi”、”bczi”、”mcfi”和”mzi”。请编程实现一个函数用来计算一个数字有多少种不同的翻译方法。

 

分析:

 

完整代码:

//给出礼物矩阵,求矩阵中能获取的礼物最大值
int getMaxValues_solution(const int* values, int rows, int cols)
{
	//判断输入的合法性
	if( values == nullptr || rows <= 0 || cols <= 0)
		return 0;
	//声明一个变量,用来存储不同位置对应的礼物价值
	int* MaxValues = new int[cols];
	
	//遍历整个矩阵
	for(int i = 0; i < rows; i++)
	{
		for(int j = 0; j < cols; j++)
		{
			//声明一个变量,计算当前位置上方的礼物价值
			int up = 0;
			int left = 0;
			//如果在当前位置存在上方值
			if(i > 0)
				up = MaxValues[j];
			//如果存在当前位置存在左边值
			if(j > 0)
				left = MaxValues[j-1];
			//计算当前位置的礼物最大值
			MaxValues[i] = max(up,left) + values[i*cols+j];	
		}
	}
	//求最终的礼物最大值
	int result = MaxValues[cols - 1];
	
	//删除MaxValues数组
	delete []MaxValues;
	
	//返回最终的结果
	return result;	
}

int main(){
    cout<<GetTranslationCount(12258)<<endl; 
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_41923658/article/details/95325028
今日推荐