Created
November 2, 2018 06:18
-
-
Save fpdjsns/135a796bd1a10b0f4625e405986c0461 to your computer and use it in GitHub Desktop.
[leetcode] 91. Decode Ways : https://leetcode.com/problems/decode-ways/
This file contains 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: | |
bool isAlph(string s){ | |
int n = s[0] - '0'; | |
if(2 == s.size()){ | |
if(n==0) return false; | |
n = n*10 + s[1] - '0'; | |
} | |
return 1 <= n && n <= 26; | |
} | |
int numDecodings(string s) { | |
int size = s.size(); | |
vector<int> d(size, 0); // d[i] = s[0~i]로 만들수 있는 문자의 경우의 수 | |
int ans = 0; | |
for(int i=0;i<size;i++){ | |
if(0 <= i-1 && isAlph(s.substr(i-1,2))){ | |
d[i] += (0 <= i-2 ? d[i-2] : 1); | |
} | |
if(isAlph(s.substr(i,1))) | |
d[i] += (0 <= i-1 ? d[i-1] : 1); | |
} | |
return d[size-1]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment