LeetCode(剑指 Offer)- 46. 把数字翻译成字符串
Posted 程序员牧码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(剑指 Offer)- 46. 把数字翻译成字符串相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:略。
解题思路
相关企业
- 字节跳动
- 微软(Microsoft)
- 谷歌(Google)
AC 代码
- Java
// 解决方案(1)
class Solution
public int translateNum(int num)
String s = String.valueOf(num);
int a = 1, b = 1;
for(int i = 2; i <= s.length(); i++)
String tmp = s.substring(i - 2, i);
int c = tmp.compareTo("10") >= 0 && tmp.compareTo("25") <= 0 ? a + b : a;
b = a;
a = c;
return a;
// 解决方案(2)
class Solution
public int translateNum(int num)
String s = String.valueOf(num);
int a = 1, b = 1;
for(int i = s.length() - 2; i > -1; i--)
String tmp = s.substring(i, i + 2);
int c = tmp.compareTo("10") >= 0 && tmp.compareTo("25") <= 0 ? a + b : a;
b = a;
a = c;
return a;
// 解决方案(3)
class Solution
public int translateNum(int num)
int a = 1, b = 1, x, y = num % 10;
while(num > 9)
num /= 10;
x = num % 10;
int tmp = 10 * x + y;
int c = (tmp >= 10 && tmp <= 25) ? a + b : a;
b = a;
a = c;
y = x;
return a;
- C++
// 解决方案(1)
class Solution
public:
int translateNum(int num)
int a = 1, b = 1, x, y = num % 10;
while(num > 9)
num /= 10;
x = num % 10;
int tmp = 10 * x + y;
int c = (tmp >= 10 && tmp <= 25) ? a + b : a;
b = a;
a = c;
y = x;
return a;
;
// 解决方案(2)
class Solution
public:
int translateNum(int num)
string s = to_string(num);
int a = 1, b = 1, len = s.size();
for(int i = 2; i <= len; i++)
string tmp = s.substr(i - 2, 2);
int c = tmp.compare("10") >= 0 && tmp.compare("25") <= 0 ? a + b : a;
b = a;
a = c;
return a;
;
// 解决方案(3)
class Solution
public:
int translateNum(int num)
string s = to_string(num);
int a = 1, b = 1, len = s.size();
for(int i = len - 2; i > -1; i--)
string tmp = s.substr(i, 2);
int c = tmp.compare("10") >= 0 && tmp.compare("25") <= 0 ? a + b : a;
b = a;
a = c;
return a;
;
以上是关于LeetCode(剑指 Offer)- 46. 把数字翻译成字符串的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode]剑指 Offer 46. 把数字翻译成字符串
[LeetCode]剑指 Offer 46. 把数字翻译成字符串
LeetCode剑指offer46把数字翻译成字符串(动态规划)
LeetCode1442. 形成两个异或相等数组的三元组数目 / 剑指 Offer 46. 把数字翻译成字符串 / 剑指 Offer 47. 礼物的最大价值