zoukankan      html  css  js  c++  java
  • Decode Ways

    Decode Ways

    问题:

    A message containing letters from A-Z is being encoded to numbers using the following mapping:

    'A' -> 1
    'B' -> 2
    ...
    'Z' -> 26

    Given an encoded message containing digits, determine the total number of ways to decode it.

    For example,
    Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).

    The number of ways decoding "12" is 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];
            if(s.charAt(0) == '0')  return 0;
            dp[0] = 1;
            if(n == 1) return 1;
            if(s.charAt(1) == '0')
            {
                if(isValid(s.substring(0,2)))
                {
                    dp[1] = 1;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                if(isValid(s.substring(0,2)))
                {
                    dp[1] = 2;
                }
                else
                {
                    dp[1] = 1;
                }
            }
            for(int i = 2; i < n; i++)
            {
                String substr = s.substring(i-1,i+1);
                if(s.charAt(i) == '0')
                {
                    if(!isValid(substr))    return 0;
                    dp[i] = dp[i-2];
                    continue;
                }
                dp[i] = dp[i-1];
                if(isValid(substr))
                {
                    dp[i] += dp[i-2];
                }
            }
            return dp[n-1];
        }
        public boolean isValid(String substr)
        {
            int sub = Integer.valueOf(substr);
            if(sub <= 26 && sub >= 10)   return true;
            return false;
        }
    }
    View Code

    他人代码:

    public class Solution {
        public int numDecodings(String s) {
            if (s == null || s.length() == 0) {
                return 0;
            }
            int[] nums = new int[s.length() + 1];
            nums[0] = 1;
            nums[1] = s.charAt(0) != '0' ? 1 : 0;
            for (int i = 2; i <= s.length(); i++) {
                if (s.charAt(i - 1) != '0') {
                    nums[i] = nums[i - 1];
                }
                
                int twoDigits = (s.charAt(i - 2) - '0') * 10 + s.charAt(i - 1) - '0';
                if (twoDigits >= 10 && twoDigits <= 26) {
                    nums[i] += nums[i - 2];
                }
            }
            return nums[s.length()];
        }
    }
    View Code

    学习之处:

    • 别人的代码更加简洁,更加清楚明了,学习多用一个存储空间解决问题,动态规划方程dp = new int[n+1]
    • 动态规划和DFS的区别,动态规划是从小问题转变成大问题,dfs是从大问题变成小问题
    • 所以说 既然知道dfs怎么写,如何一步步的减小问题,那么反推过来,逆向思维,就应该知道动态规划的方程是什么样子的了。
    • 想不出来动态规划方程的时候,想想dfs是怎么操作的(灵光一现的思路)
  • 相关阅读:
    SSM框架整合(Spring+SrpingMVC+Mybatis) 简单案例
    SpringDataRedis操作Redis简单案例
    SpringMVC总结四:拦截器简单介绍
    Configure a bridge interface over a VLAN tagged bonded interface
    Create a bridge using a tagged vlan (8021.q) interface
    Configure a VLAN (on top of a bond) with NetworkManager (nmcli) in RHEL7
    Configure bridge on a team interface using NetworkManager in RHEL 7
    Configure a bridged network interface for KVM using RHEL 5.4 or later?
    程序员的成长阶梯和级别定义
    <程序员从入门到精通> -- How
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4374762.html
Copyright © 2011-2022 走看看