作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/decode-ways/solution/jie-ma-fang-fa-by-leetcode-solution-p8np/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
我真的是受不了了,这些“巧妙”的方法,实在是太秀了。我最近开始学习前端了。后端不适合我......html,css,javascript最近开始学了,有点晚了,但是也要赶紧学起来......
个人微改:
class decode{
public int numDecodings(String s) {
int n = s.length();
int f[] = new int[n+1];
f[0]=1;
for(int i=1;i<=n;i++) {
if(s.charAt(i-1)!='0') {
f[i] += f[i-1];
}
if(i>1 && (s.charAt(i-2)!='0') && ((s.charAt(i-2)-'0')*10+(s.charAt(i-1)-'0'))<=26) {
f[i] += f[i-2];
}
}
return f[n];
}
}
public class DecodeWays {
public static void main(String args[]) {
String str1 = "226";
String str2 = "106";
String str3 = "0";
decode hah = new decode();
System.out.println(hah.numDecodings(str1));
System.out.println(hah.numDecodings(str2));
System.out.println(hah.numDecodings(str3));
}
}

本文解析了如何使用Java解决LeetCode上的解码字符串问题,通过动态规划方法计算合法编码方式数。适合初学者理解编码与解码的技巧应用。

被折叠的 条评论
为什么被折叠?



