- 【LeetCode】剑指 Offer 46. 把数字翻译成字符串
package offer; public class Solution46 { public static void main(String[] args) { int num = 12258; Solution46 solution = new Solution46(); System.out.println(solution.method(num)); } //动态规划 private int method(int num){ int a = 1, b = 1, c, x, y = num % 10, temp; while(num != 0){ num /= 10; x = num % 10; temp = x * 10 + y; c = (temp >= 10 && temp <= 25) ? a + b : a; b = a; a = c; y = x; } return a; } } //时间复杂度为 O(n) //空间复杂度为 O(1)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)