// 动态规划解 时复空复O(n) classSolution{ publicinttranslateNum(int num){ int a = 1, b = 1; String s = String.valueOf(num); 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:b; a = b; b = c; } return b; } }
// 求余解 时复O(n) 空复O(1) classSolution{ publicinttranslateNum(int num){ int a = 1, b = 1, y = num%10, x; while(num != 0){ 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; } }