面试题分享:把数字翻译成字符串# JobHunting - 待字闺中
z*t
1 楼
写了篇博客讨论下面的面试题:
Given a number, please translate it to a string, following the rules: 1 is
translated to 'a', 2 to 'b', …, 12 to 'l', …, 26 to 'z'. For example, the
number 12258 can be translated to "abbeh", "aveh", "abyh", "lbeh" and "lyh",
so there are 5 different ways to translate 12258. How to write a function/
method to count the different ways to translate a number?
博客URL:http://codercareer.blogspot.com/2014/09/no-55-translating-numbers-to-string.html
这个题目用递归分析,不难找出解题思路。但有两点细节需要注意:
1、递归的子问题有可能重复。写代码时要确保子问题不会被重复计算;
2、题目没有明确说0怎么翻译,那么诸如数字101等包含0的数字怎么翻译?这可以和面
试官讨论。这可能是面试官考查沟通能力的地方。
Given a number, please translate it to a string, following the rules: 1 is
translated to 'a', 2 to 'b', …, 12 to 'l', …, 26 to 'z'. For example, the
number 12258 can be translated to "abbeh", "aveh", "abyh", "lbeh" and "lyh",
so there are 5 different ways to translate 12258. How to write a function/
method to count the different ways to translate a number?
博客URL:http://codercareer.blogspot.com/2014/09/no-55-translating-numbers-to-string.html
这个题目用递归分析,不难找出解题思路。但有两点细节需要注意:
1、递归的子问题有可能重复。写代码时要确保子问题不会被重复计算;
2、题目没有明确说0怎么翻译,那么诸如数字101等包含0的数字怎么翻译?这可以和面
试官讨论。这可能是面试官考查沟通能力的地方。