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]; } }
时间: 2024-10-07 01:29:49