Jan 23 - Decode Ways; DP; Array;
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jan 23 - Decode Ways; DP; Array;相关的知识,希望对你有一定的参考价值。
public class Solution { public int numDecodings(String s) { int len = s.length(); if(len == 0) return 0; int[] counts = new int[len]; counts[0] = 0; for(int i = 0; i < len; i++){ int flag = 0; if(i == 0) { if(s.charAt(i) != ‘0‘) counts[i] = 1; else counts[i] = 0; continue; } if(i == 1){ int mes = Integer.parseInt(s.charAt(i-1)+""+s.charAt(i)); if(mes >= 10 && mes <= 26) { flag++; } if(s.charAt(i) != ‘0‘) flag++; if(flag == 2) counts[i] = counts[i-1]+1; if(flag == 1) counts[i] = counts[i-1]; if(flag == 0) counts[i] = 0; continue; } int mes = Integer.parseInt(s.charAt(i-1)+""+s.charAt(i)); if(mes >= 10 && mes <= 26) { flag++; counts[i] = counts[i-2]; } if(s.charAt(i) != ‘0‘) { flag++; counts[i] = counts[i-1]; } if(flag == 2) counts[i] = counts[i-2]+counts[i-1]; } return counts[len-1]; } }
以上是关于Jan 23 - Decode Ways; DP; Array;的主要内容,如果未能解决你的问题,请参考以下文章