目录
- 删除链表中重复的节点
- 二叉搜索树的第k个节点
- 字符串循环左移k位
- 整数区间中1出现的次数
- 二叉树中和为某一值的路径
- 青蛙跳台阶(斐波那契)
- 用递归的方法绘制带绿叶的小树
- 绘制谢尔宾斯基三角形
0.1删除链表中重复的节点
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9dc144da49a7f57db1b33e3da3c24aa7.png)
class LinkedNode:
def __init__(self, x):
self.val = x
self.next = None
def delete(pHead):
nodeValue = {}
newHead = LinkedNode(pHead.val)
nodeValue[pHead.val] = pHead.val
curNode = newHead
while pHead.next:
pHead = pHead.next
if nodeValue.get(pHead.val) == None:
curNode.next = LinkedNode(pHead.val)
curNode = curNode.next
nodeValue[pHead.val] = pHead.val
return newHead
header = LinkedNode(5)
node1 = LinkedNode(5)
header.next = node1
node2 = LinkedNode(10)
node1.next = node2
node3 = LinkedNode(3)
node2.next = node3
def printLinked(header):
p = header
while p:
print(p.val, end=' ')
p = p.next
header1 = delete(header)
printLinked(header1)
0.2二叉搜索树的第k个节点
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2d619ac8389645d8a2b1725499b14108.png)
中序遍历
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class KNode:
def KthNode(self, pRoot, k):
global result
result = []
middle = self.midorder(pRoot)
if k <= 0 or len(middle) < k:
return None
else:
return middle[k - 1]
def midorder(self, pRoot):
if not pRoot:
return []
self.midorder(pRoot.left)
result.append(pRoot)
self.midorder(pRoot.right)
return result
root = TreeNode(10)
left = TreeNode(6)
right = TreeNode(15)
root.left = left
root.right = right
left1 = TreeNode(11)
right1 = TreeNode(20)
right.left = left1
right.right = right1
print(KNode().KthNode(root, 4).val)
15
0.3字符串循环左移k位
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9b753a9ab248aadb876b23bfbbc60e68.png)
def leftmove(s, k):
return s[k:] + s[:k]
def rightmove(s, k):
return s[len(s) - k:] + s[:len(s) - k]
print(leftmove('abcXYZopq', 3))
print(rightmove('abcXYZopq', 3))
0.4整数区间中1出现的次数
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1ddc71a4af65b4e63ee0cdad88a7c3af.png)
def findone(n):
count = 0
for i in range(1, n+1):
for i in str(i):
if i == '1':
count +=1
return count
print(findone(200))
140
0.5二叉树中和为某一值的路径
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c2b94efa13a55cdfaf3c57e763065304.png)
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def findpath(root, n):
if not root:
return []
result = []
def findpath2(root, path, curNum):
curNum += root.val
path.append(root)
flag = root.left == None and root.right == None
if curNum == n and flag:
onepath = []
for node in path:
onepath.append(node.val)
result.append(onepath)
if curNum < n:
if root.left:
findpath2(root.left, path, curNum)
if root.right:
findpath2(root.right, path, curNum)
path.pop()
findpath2(root, [], 0)
return result
root = TreeNode(10)
left = TreeNode(6)
right = TreeNode(15)
root.left = left
root.right = right
left1 = TreeNode(11)
right1 = TreeNode(20)
right.left = left1
right.right = right1
print(findpath(root, 16))
[[10, 6]]
0.6青蛙跳台阶(斐波那契数列)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/929584a8599eacad49539337d08c5d1e.png)
'''
总共 n 级台阶,剩下 f(n) 级
1.第一次跳 1 级,剩下 f(n-1) 级
2.第一次跳 2 级,剩下 f(n-2) 级
第一次跳就这两个方法,总共 f(n-1) + f(n-2) 种方法
f(1) = 1
f(2) = 2
斐波那契数列
'''
def jumpfloor(n):
res = [1, 2]
while len(res) <= n:
res.append(res[-1] + res[-2])
if n == 1:
return 1
else:
return res[n - 1]
print(jumpfloor(20))
10946
0.7用递归的方法绘制带绿叶的小树
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fbacfc6b3d2aabc68582ca72eba1f09c.png)
import turtle
def tree(branch_len, t):
if branch_len > 5:
t.forward(branch_len)
if (branch_len - 15) <= 5:
t.pencolor('green')
else:
t.pencolor('black')
new_width = branch_len // 5
if new_width > 0:
t.pensize(new_width)
t.right(20)
tree(branch_len - 15, t)
if new_width > 0:
t.pensize(new_width)
t.left(40)
tree(branch_len - 15, t)
t.right(20)
t.backward(branch_len)
t = turtle.Turtle()
win = turtle.Screen()
t.left(90)
t.pencolor('black')
t.pensize(15)
tree(70, t)
win.exitonclick()
0.8绘制谢尔宾斯基三角形
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d283abd06263e1e0d94201b5168e8903.png)
import turtle
def draw_triangle(points, color, t):
t.fillcolor(color)
t.up()
t.goto(points[0][0], points[0][1])
t.down()
t.begin_fill()
t.goto(points[1][0], points[1][1])
t.goto(points[2][0], points[2][1])
t.goto(points[0][0], points[0][1])
t.end_fill()
def mid_point(p1, p2):
return ((p1[0] + p2[0])/2, (p1[1] + p2[1])/2)
def sterpin(points, degree, t):
color = ['blue', 'green', 'red', 'yellow', 'orange', 'violet']
draw_triangle(points, color[degree], t)
if degree >= 0:
sterpin([points[0], mid_point(points[0], points[1]), mid_point(points[0], points[2])], degree - 1, t)
sterpin([points[1], mid_point(points[0], points[1]), mid_point(points[1], points[2])], degree - 1, t)
sterpin([points[2], mid_point(points[2], points[1]), mid_point(points[0], points[2])], degree - 1, t)
t = turtle.Turtle()
win = turtle.Screen()
points = [[-200, -100], [0, 200], [200, -100]]
sterpin(points, 4, t)
win.exitonclick()