Created
January 27, 2019 22:29
-
-
Save ttsugriy/f7d2b2671a90d228e53e9b7358f32253 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int numDecodings(const string& s) { | |
| if (s.empty()) return 0; | |
| if (s.size() == 1) return s[0] != '0'; | |
| int n_2 = s.back() != '0'; | |
| int n_1 = (s[s.size()-2] != '0') * n_2 + (s[s.size()-2] == '1' || s[s.size()-2] == '2' && s.back() <= '6'); | |
| for (int i = s.size()-3; i >= 0; --i) { | |
| int num = (s[i] > '0') * n_1 + (s[i] == '1' || s[i] == '2' && s[i+1] <= '6') * n_2; | |
| n_2 = n_1; | |
| n_1 = num; | |
| } | |
| return n_1; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment