Guest User

Untitled

a guest
Oct 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. public int numDecodings(String s) {
  2. final int len = s.length();
  3.  
  4. int pre = 1, sum = s.charAt(0) == '0' ? 0 : 1, tmp;
  5. if (sum == 0) return 0;
  6. for (int i = 1; i < len; i++) {
  7. tmp = sum;
  8. if (s.charAt(i - 1) == '1' || (s.charAt(i - 1) == '2' && s.charAt(i) <= '6')) {
  9. if (s.charAt(i) == '0')
  10. sum = pre;
  11. else
  12. sum += pre;
  13. } else if (s.charAt(i) == '0') {
  14. return 0;
  15. }
  16. pre = tmp;
  17. }
Add Comment
Please, Sign In to add comment