decode ways
// dfs class Solution { public int numDecodings(String s) { if(s == null || s.length() == 0) return 0; return dfs(s, 0); } private int dfs(String s, int i){ if(i == s.length()) return 1; if(s.charAt(i) == '0') return 0; if(i < s.length() - 1 && Integer.valueOf(s.substring(i, i + 2)) < 26){ return dfs(s, i + 1) + dfs(s, i + 2); } return dfs(s, i + 1); } } // dp version 1 class Solution { public int numDecodings(String s) { if(s.length() == 0 || s.charAt(0) == '0') return 0; int[] dp = new int[s.length() + 1]; dp[0] = 1; dp[1] = 1; for(int i = 1; i < s.length(); i++){ if(s.charAt(i) != '0'){ dp[i + 1] = dp[i]; } if(s.charAt(i - 1) != '0' && Integer.valueOf(s.substring(i-1, i+1)) <= 26){ dp[i+1] += dp[i-1]; } } return dp[s.length()]; } } // dp version 2 public class Solution{ public int numDecodings(String s){ if(s == null || s.length() == 0){ return 0; } int n = s.length(); int[] dp = new int[n+1]; dp[0] = 1; if(s.charAt(0) != '0'){ dp[1] = 1; }else{ dp[1] = 0; } for(int i = 2; i <= n; i++){ int first = Integer.valueOf(s.substring(i-1, i)); int second = Integer.valueOf(s.substring(i-2, i)); if(first >= 1 && first <= 9){ dp[i] += dp[i-1]; } if(second >= 10 && second <= 26){ dp[i] += dp[i-2]; } } return dp[n]; } } // dp + space optimization class Solution { public int numDecodings(String s) { if(s.length() == 0 || s.charAt(0) == '0') return 0; int dp2 = 1; int dp1 = 1; for(int i = 1; i < s.length(); i++){ if(s.charAt(i) == '0') { dp1 = 0; } int tmp = 0; int part = Integer.valueOf(s.substring(i - 1, i + 1)); if(part <= 26){ tmp = dp1 + dp2; }else{ tmp = dp1; } dp2 = dp1; dp1 = tmp; } return dp1; } } // return all decoded ways 4. Return all decode ways . Dfs package com.jetbrains; import java.util.*; public class Main { public List<String> numDecodings(String s){ List<String> res = new ArrayList<>(); if(s == null || s.length() == 0){ return res; } dfs(res, s, new StringBuilder(), 0); return res; } private void dfs(List<String> res, String s, StringBuilder sb, int i){ if(i == s.length()){ res.add(sb.toString()); return; } if(s.charAt(i) == '0'){ return; } int length = sb.length(); int num1 = Integer.valueOf(s.substring(i, i + 1)); dfs(res, s, sb.append((char)('A' + num1 - 1)), i + 1); sb.setLength(length); if(i < s.length() - 1 && Integer.valueOf(s.substring(i, i + 2)) <= 26){ int num2 = Integer.valueOf(s.substring(i, i + 2)); dfs(res, s, sb.append((char)('A' + num2 - 1)), i + 2); sb.setLength(length); } } public static void main(String[] args) { Main solution = new Main(); String w = "226"; List<String> result = solution. numDecodings(w); System.out.println("the result we got from this code is " + result); } } //the result we got from this code is [BBF, BZ, VF] Decode ways visulization 102213 ————— 5 10221 3 1022 13 \\\\\\\\\ 10221————— 3 1022 1 102 21 \\\\\\\\ 1022 —————— 2 102 2 10 22 \\\\\ 102———— 1 10 2 1 02 \\\\\\\\ 10—————— 1 1 0 10 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a non-empty string containing only digits, determine the total number of ways to decode it. Example 1: Input: "12" Output: 2 Explanation: It could be decoded as "AB" (1 2) or "L" (12). Example 2: Input: "226" Output: 3 Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
posted on 2018-09-08 07:33 猪猪🐷 阅读(124) 评论(0) 收藏 举报
浙公网安备 33010602011771号