 # [刷题记录板] [leetcode] （Dec,13）每天两道题目，配 python参考解答

20主题 586积分 586 发表于 12-13-2014 02:10 PM | 显示全部楼层 |阅读模式

### 亲！马上注册或者登录会查看更多内容！

x

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.

The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.

For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2

Note:
For a given n, a gray code sequence is not uniquely defined.
For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.
For now, the judge is able to judge based on one instance of gray code sequence. Sorry about that.  python参考解答：

[code]class Solution:
# @param s, a string
# @return an integer
def numDecodings(self, s):
if s=="" or s=='0': return 0
dp=[1,1]
for i in range(2,len(s)+1):
if 10 <=int(s[i-2:i]) <=26 and s[i-1]!='0':
dp.append(dp[i-1]+dp[i-2])
elif int(s[i-2:i])==10 or int(s[i-2:i])==20:
dp.append(dp[i-2])
elif s[i-1]!='0':
dp.append(dp[i-1])
else:
return 0
return dp[len(s)][/code]

[code]class Solution:
# @return a list of integers
def grayCode(self, n):
res=[]
size=1<         for i in range(size):
res.append((i>>1)^i)
return res[/code]

### 本帖被以下淘专辑推荐:

 本版积分规则 回帖后跳转到最后一页   