标签:== using 不为 [1] 组成 numbers pac nbsp art
public class Solution { public int NumDecodings(string s) { if(string.IsNullOrWhiteSpace(s)){ return 0; } if(s.Length == 1){ return s == "0" ? 0 : 1; } if(s[0] == ‘0‘ || s[0] >= ‘3‘&& s[1] == ‘0‘){ return 0; } if(s.Length == 2){ return s[1] == ‘0‘ || !ok(s[0],s[1]) ? 1 : 2; } var arr = new int[s.Length]; arr[0] = 1; arr[1] = s[1] == ‘0‘ || !ok(s[0], s[1]) ? 1 : 2; for(var i = 2;i < s.Length; i++){ if(s[i-1] == ‘0‘ && s[i] == ‘0‘){ return 0; } if(s[i] != ‘0‘){ arr[i] += arr[i-1]; } if(ok(s[i-1], s[i])){ arr[i] += arr[i-2]; } } return arr[s.Length-1]; } private bool ok(char c1 , char c2){ if(c1 == ‘0‘){ return false; } if (c1 == ‘1‘ || c1 == ‘2‘ && c2 <= ‘6‘){ return true; } return false; } }
标签:== using 不为 [1] 组成 numbers pac nbsp art
原文地址:http://www.cnblogs.com/slgkaifa/p/6851562.html