`
testforvln
  • 浏览: 18937 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • superich2008: 1、去重可以用Set集合2、在排序后,相邻2个元素如果相同可以 ...
    4Sum

Decode Ways

 
阅读更多
public class Solution {
    public int numDecodings(String s) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if (s.length() == 0)
            return s.length();
        else if (s.startsWith("0"))
            return 0;
        else if (s.length() == 1){
            return 1;
        }
        
        else {
            int sum = 0; 
            int temp = Integer.parseInt(s.substring(0,2));
            if (temp <= 26 && temp > 0)
                if (s.length() > 2)
                    sum += numDecodings(s.substring(2, s.length()));
                else
                    sum += 1;
            sum += numDecodings(s.substring(1, s.length()));
            return sum;
        }
    }
}

递归的方法,就是需要注意0开头的字符串是不能被parse的。但是。。。。大数据超时了??唉。。。
看了代码,发现确实有这个问题,numDecodings(s.substring(1, s.length()))还会重复计算之前计算过的numDecodings(s.substring(2, s.length()))
那么只能逆着递推了(类似动规?)
public class Solution {
    public int numDecodings(String s) {
        // Start typing your Java solution below
        // DO NOT write main() function
        int length = s.length();
        if (length == 0)
            return 0;
        int[] count = new int[length+1];
        count[length] = 1;
        if (s.charAt(length-1) =='0')
            count[length-1] = 0;
        else
            count[length-1] = 1;
        for (int i = length-2; i >=0; --i){
            if (s.charAt(i) != '0')
                count[i] += count[i+1];
            else
                continue;
            int temp = Integer.parseInt(s.substring(i,i+2));
            if (temp <=26)
                count[i] += count[i+2];
        }
        return count[0];
    }
}
分享到:
评论

相关推荐

    js代码-leetcode 91 DEcode Ways

    js代码-leetcode 91 DEcode Ways

    javalruleetcode-learn-algorithms::laptop:Java实现的各种算法题解

    Ways](./leetcode/动态规划-Decode Ways.java) [动态规划-Distinct Subsequences](./leetcode/动态规划-Distinct Subsequences.java) [动态规划-Longest Valid Parentheses](./leetcode/动态规划-Longest Valid ...

    cpp-算法精粹

    Decode Ways Distinct Subsequences Word Break Word Break II Dungeon Game House Robber House Robber II House Robber III Range Sum Query - Immutable Range Sum Query 2D - Immutable 图 Clone Graph 位操作 ...

    leetcode分类-LeetCode:力码

    leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 ...Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar

    gasstationleetcode-leetcode:LeetcodeOJ解决方案

    加油站 leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167....2107/03/06 15.3 总和,16.3 ...91.Decode Ways, 96.Unique Binary Search Tree, 120.Triangle, 139.Word Break, 152.Maximum Produ

    leetcode怎么计算空间复杂度是指-LeetCode-Solution:我的第一个LeetCode解决方案

    leetcode怎么计算空间复杂度是指 LeetCode-Solution my first solution of LeetCode 2015-5-7 Problem 95,98(80 ...我经常在递归的结束地方忘记return!...091:Decode Ways 简单的一维DP,用额外数组O(n)即可。 139,1

    扩展矩阵leetcode-interview-algorithm:面试算法

    扩展矩阵leetcode interview-algorithm leetCode 待解决 上楼梯问题 how many ways to decode this message @leetCode 91

    Python for Secret Agents

    Python is an easy-to-learn and extensible programming language that allows secret agents to work with a wide variety of data in a number of ways. It gives beginners a simple way to start programming, ...

    LeetCode刷题——91. 解码方法

    题目 一条包含字母 A-Z 的消息通过以下方式进行了编码: ‘A’ -&gt; 1 ‘B’ -&gt; 2 … ‘Z’ -&gt; 26 给定一个只包含数字的非空字符串...链接:https://leetcode-cn.com/problems/decode-ways 思路 这个题有点像爬楼梯,但是

    Python.for.Secret.Agents.2nd.Edition.1785283405

    Gather, analyze, and decode data to reveal hidden facts using Python, the perfect tool for all aspiring secret agents About This Book Discover the essential features of Python programming: statements...

    世界上最快的VP9视频解码器

    There’s basically two ways to measure this: same-bitrate (e.g. a 500kbps VP8 file vs. a 500kbps VP9 file, where the VP9 file likely looks much better), or same-quality (e.g. a VP8 file with SSIM=...

    Sakemail

    1997 - 2003 Sergio A....and fixed it. Some minor bugs that I don‘t remember fixed.- Added MIME-compliant base64 support (not for use by now). Added examples.0.9.2.1b- Fixed a bug when send a mail and ...

    esp8266 mp3

    Yes, this possibly is one of the worst ways to do this, //but RAM is at a premium here, and this works for most of the cases. int ICACHE_FLASH_ATTR openConn(const char *streamHost, const char *...

Global site tag (gtag.js) - Google Analytics