Skip to content

Commit 37da9b4

Browse files
something new
1 parent 3260d1c commit 37da9b4

4 files changed

Lines changed: 175 additions & 68 deletions

File tree

.idea/workspace.xml

Lines changed: 93 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33

44
## update 20160704
55
准备加入[《剑指offer》](https://www.amazon.cn/%E5%9B%BE%E4%B9%A6/dp/B00L5LKMVU?ie=UTF8&*Version*=1&*entries*=0)的习题python实现,以及机器学习过程中的一些算法
6+
7+
## update 20160717
8+
加入leetcode部分

Target Offer/格雷码.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同, 则称这种编码为格雷码(Gray Code),请编写一个函数,使用递归的方法生成N位的格雷码。
3+
给定一个整数n,请返回n位的格雷码,顺序为从0开始。
4+
'''
5+
6+
# -*- coding:utf-8 -*-
7+
8+
class GrayCode:
9+
def getGray(self, n):
10+
L = []
11+
L1 = []
12+
L2 = []
13+
if n == 1:
14+
L = ['0', '1']
15+
else:
16+
L1 = self.getGray(n-1)
17+
L2 = L1[::-1]
18+
num = len(L1)
19+
for i in range(num):
20+
L1[i] = '0' + L1[i]
21+
L2[i] = '1' + L2[i]
22+
L = L1 + L2
23+
return L
24+
s = GrayCode()
25+
print(s.getGray(3))

0 commit comments

Comments
 (0)