本文主要是介绍LeetCode 题解(15): Decode Ways,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
A message containing letters from A-Z
is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message "12"
,it could be decoded as "AB"
(1 2) or"L"
(12).
The number of ways decoding "12"
is 2.
题解:
动态规划,写的十分之丑。
class Solution {
public:int numDecodings(string s){if(!s.length() || s[0] == '0')return 0;int* count = new int[s.length()];count[0] = 1;for(int i = 1; i < s.length(); i++){if(i < s.length() - 1 && s[i] == '0' && s[i+1] == '0') //连续两个00的情况,无意义,直接返回0return 0;else if(s[i] == '0' && s[i-1] > '2')//30,40,50...90的情况,无意义,直接返回0return 0;else if(s[i] == '0' && i - 2 >= 0)//s = '110'count[i] = count[i-2];else if(s[i] == '0' )count[i] = count[i-1];else if(i - 2 >= 0 && s[i-1] != '0' && strToInt(s.substr(i-1,2)) <= 26)count[i] = count[i-1] + count[i-2];else if(s[i-1] != '0' && strToInt(s.substr(i-1,2)) <= 26)count[i] = count[i-1] + 1;elsecount[i] = count[i-1];}return count[s.length()-1];}int static strToInt(string s){assert(s.length() == 2);int result = (s[0]-'0') * 10 + s[1]-'0';return result;}
};
若干个月后重做,写的愈发丑。
class Solution {
public:int numDecodings(string s) {if(s.length() == 0 || s[0] == '0')return 0;if(s.length() == 1)return 1;int* ways = new int[s.length()];ways[0] = 1;if(stoi(s.substr(0,2)) == 10 || stoi(s.substr(0,2)) == 20) ways[1] = 1;else if(stoi(s.substr(0,2)) >= 11 && stoi(s.substr(0,2)) <= 26)ways[1] = 2;else if(s[1] == '0' && s[0] - '3' >= 0)return 0;elseways[1] = 1;for(int i = 2; i < s.length(); i++) {if(s[i] == '0' && (s[i-1] == '0' || s[i-1] - '3' >= 0))return 0;else if(s[i] == '0')ways[i] = ways[i-2];else if(stoi(s.substr(i-1,2)) <= 9)ways[i] = ways[i-1];else if(stoi(s.substr(i-1,2)) <= 26)ways[i] = ways[i-2]+ways[i-1];else ways[i] = ways[i-1];}return ways[s.length()-1];}};
Java版:写的更是丑。
public class Solution {public int numDecodings(String s) {if(s.length() == 0)return 0;if(s.length() == 1 && s.charAt(0) != '0')return 1;int[] ways = new int[s.length()];if(s.charAt(0) == '0')return 0;elseways[0] = 1;if(s.length() > 2)if((Integer.parseInt(s.substring(0,2)) >= 11 && Integer.parseInt(s.substring(0,2)) <= 19) || (Integer.parseInt(s.substring(0,2)) >= 21 && Integer.parseInt(s.substring(0,2)) <= 26))ways[1] = 2;else if(s.charAt(1) == '0' && s.charAt(0) - '3' >= 0)return 0;elseways[1] = 1; else if(s.length() == 2)if((Integer.parseInt(s.substring(0)) >= 11 && Integer.parseInt(s.substring(0)) <= 19) || (Integer.parseInt(s.substring(0)) >= 21 && Integer.parseInt(s.substring(0)) <= 26))ways[1] = 2;else if(s.charAt(1) == '0' && s.charAt(0) - '3' >= 0)return 0;elseways[1] = 1; for(int i = 2; i < s.length(); i++) {if((s.charAt(i-1) == '0' && s.charAt(i) == '0') || (s.charAt(i) == '0' && s.charAt(i-1) - '3' >= 0))return 0;if(s.charAt(i-1) == '0' || (s.charAt(i) == '0' && s.charAt(i-1) - '2' <= 0))ways[i] = ways[i-2];else if((Integer.parseInt(s.substring(i-1, i+1)) >= 11 && Integer.parseInt(s.substring(i-1, i+1)) <= 19) || (Integer.parseInt(s.substring(i-1, i+1)) >= 21 && Integer.parseInt(s.substring(i-1, i+1)) <= 26))ways[i] = ways[i-1] + ways[i-2];elseways[i] = ways[i-1];}return ways[s.length()-1];}
}
Python版:
class Solution:# @param s, a string# @return an integerdef numDecodings(self, s):if len(s) == 0:return 0if len(s) == 1 and s[0] != '0':return 1ways = ['0'] * len(s)if s[0] == '0':return 0else:ways[0] = 1if (int(s[0]+s[1]) >= 11 and int(s[0]+s[1]) <= 19) or (int(s[0]+s[1]) >= 21 and int(s[0]+s[1]) <= 26):ways[1] = 2elif int(s[0]) >= 3 and s[1] == '0':return 0else:ways[1] = 1for i in range(2, len(s)):if (s[i-1] == '0' and s[i] == '0') or (int(s[i-1]) >= 3 and s[i] == '0'):return 0elif s[i] == '0':ways[i] = ways[i-2]elif (int(s[i-1]+s[i]) >= 11 and int(s[i-1]+s[i]) <= 19) or (int(s[i-1]+s[i]) >= 21 and int(s[i-1]+s[i]) <= 26):ways[i] = ways[i-2] + ways[i-1]else:ways[i] = ways[i-1]return ways[len(s)-1]
这篇关于LeetCode 题解(15): Decode Ways的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!