GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — LeetCode/L091_Decode_Ways.java at master · likevson/LeetCode Expand file tree/ Copy pathL091_Decode_Ways.javaMore file actions 44 lines (32 loc) · 778 Bytes / Copy pathL091_Decode_Ways.javaFile metadata and controls44 lines (32 loc) · 778 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 package LeetCode; public class L091_Decode_Ways { public int numDecodings(String s) { if (s == null || s.length() == 0) { return 0; } int n = s.length(); char[] c = s.toCharArray(); // 对于台阶,需要前两步的值,所以数组最小是3 int[] step = new int[Math.max(n + 1, 3)]; step[0] = 1; step[1] = 0; // 第一个字符不是0,则第一步初始为1 if (c[0] != '0') { step[1] = 1; } // step[i] = step[i - 1] + step[i - 2]; // 只不过加step[i - 2]时,需要对c[i - 2]和c[i - 1]判断,组合是否<=26 for (int i = 2; i <= n; i++) { step[i] = 0; if (c[i - 1] != '0') { step[i] += step[i - 1]; } if (c[i - 2] != '0') { if ((c[i - 2] - '0') * 10 + (c[i - 1] - '0') <= 26) { step[i] += step[i - 2]; } } } return step[n]; } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |