【leetcode】Decode Ways(medium)
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.
做了好久才AC,主要是脑子不清楚,思路不清晰。虽然最后想通了但花了一整个下午。
思路:
f(n) 表示,从第n个字符起的字符串可以排列的方式
从后向前判断,如果该数字是0,则只有0种方式;如果数字非0,则该位数字单独解码,方式有f(n - 1)种。如果该数字可以和它后面的数字一起解码,则该数字和他后面的数字一起,方式再加上f(n-2)种。
class Solution {
public:
    int numDecodings(string s) {
        int iway[] = {,};
        int slen = s.length();
        if(slen <= )
            return ;
        iway[] = (s[slen - ] == '') ?  : ;
        for(int i = s.length() - ; i >= ; i--)
        {
            int curWays = ;
            if(s[i] != '')
            {
                curWays += iway[];
            }
            int num = (s[i] - '') *  + (s[i + ] - '');
            if( <= num && num <= )
            {
                curWays += iway[];
            }
            iway[] = iway[];
            iway[] = curWays;
        }
        return iway[];
    }
};
大神更加简洁的代码,思路差不多,就是从前向后判断的:
int numDecodings(string s) {
    // empty string or leading zero means no way
    if (!s.size() || s.front() == '') return ;
    // r1 and r2 store ways of the last and the last of the last
    int r1 = , r2 = ;
    for (int i = ; i < s.size(); i++) {
        // zero voids ways of the last because zero cannot be used separately
        if (s[i] == '') r1 = ;
        // possible two-digit letter, so new r1 is sum of both while new r2 is the old r1
        if (s[i - ] == '' || s[i - ] == '' && s[i] <= '') {
            r1 = r2 + r1;
            r2 = r1 - r2;
        }
        // one-digit letter, no new way added
        else {
            r2 = r1;
        }
    }
    return r1;
}
【leetcode】Decode Ways(medium)的更多相关文章
- 【leetcode】Decode Ways
		
题目如下: 解题思路:这个题目的本质是一个爬楼梯问题,在爬楼梯问题中,每次可以选择走一步或者两步.而本题也是一样的,解码的时候选择解码一个字符或者两个字符,但是加大了一点点难度,要考虑这些情况.1,Z ...
 - 【leetcode】Single Number (Medium) ☆
		
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
 - 【LeetCode】动态规划(下篇共39题)
		
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
 - 【LeetCode】字符串 string(共112题)
		
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
 - 【LeetCode】91. Decode Ways 解题报告(Python)
		
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
 - 【LeetCode】 454、四数之和  II
		
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
 - 【LeetCode】18、四数之和
		
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
 - 【LeetCode】15、三数之和为0
		
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
 - 【LeetCode】714、买卖股票的最佳时机含手续费
		
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
 
随机推荐
- [原] Intellij IDEA开发Android,祝还在使用eclipse的早日脱离苦海
			
注: 现在推荐使用Android Studio,以后google在Android Studio上个性差异化的东西越来越多, 所以越早使用Android Studio越好,看看更新文档,使我们开发更方便 ...
 - [设计模式] Javascript 之 观察者模式
			
观察者模式:定议 定义对象间的一种一对多的关系,当一个对象状态改变时 (一般称为被观察者),依赖于该对象的对象被通知,并更新; 观察者模式:说明 1. 观察者模式是行为模式,也被称为:发布-订阅模式. ...
 - PHP的扩展类 mysqli_stmt:预处理类
			
mysqli和mysqli_result能完成的功能 都可以使用mysqli_stmt类开完成 1.编译一次,使用多次,类似于存储过程 2.参数化查询,可防止sql注入 1: <?php 2: ...
 - 1-File类的使用
			
package com.io; import java.io.File; import java.io.FileInputStream; import java.io.IOException; imp ...
 - C#网络爬虫 WebUtility使用 转义字符 urlCode
			
背景: 在C#写网络爬虫时候,有时候需要将html中的转义字符进行处理,还有网址中的中文处理 一.html转义字符处理 1.ASP.NET中的html解析 HttpUtility.HtmlDecode ...
 - Java Annotation自定义注解详解
			
在开发过程中总能用到注解,但是从来没有自己定义过注解.最近赋闲在家,研究整理了一番,力求知其然知其所以然. 本文会尝试描述什么是注解,以及通过一个Demo来说明如何在程序中自定义注解.Demo没有实际 ...
 - Java多线程基础知识(五)
			
一. Java中的13个原子操作类 在Jdk1.5中,这个包中的原子操作类提供了一种用法简单,性能高效,线程安全的更新一个变量的方式. 1. 原子更新基本类型类 AtomicBoolean : 原子更 ...
 - BZOJ3277——串
			
0.题意:给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串(注意包括本身). 1.分析:这个题我问了吴大爷做法 首先建立后缀自动机,然后利用离线搞出每一个 ...
 - UNTIY3D接入91SDK的办法
			
原地址: http://bbs.18183.com/thread-111324-1-1.html UNITY3D接入Android-SDK 方法一:把UNITY3D游戏打成安卓项目文件,修改安卓项目文 ...
 - SpringMVC基础入门
			
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...