设为首页 加入收藏

TOP

LeetCode| Decode ways
2015-11-21 01:05:15 来源: 作者: 【 】 浏览:3
Tags:LeetCode 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.

思路:

这也是一个典型的DP问题,首先定义一个数组,dp[i]为到第i个字符所能组成的所有编码方式的个数。那么对于dp[i+1]来说,肯定至少和dp[i]一样多,如果第i个字符和i+1个字可以合成一个字符,那么dp[i+1] += dp[i-1]。不过这里需要注意的是违规字符。

?

#include 
  
   
#include 
   
     #include 
    
      using namespace std; int Decode_num(string& str) { vector
     
       vec(str.size(),0); if(str.size() <2) return 1; vec[0] =1; if(str[0]=='1' || str[1]<='6') vec[1] =2; int i; int tmp; for(i=2;i
      
       

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇[LeetCode 147]Insertion Sort Li.. 下一篇HDU_2846_Repository

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: