-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsum_solution.py
82 lines (62 loc) · 1.78 KB
/
sum_solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
# author: Xiguang Liu<[email protected]>
# 2018-05-03 15:55
# 题目描述:https://www.nowcoder.com/practice/7a0da8fc483247ff8800059e12d7caf1?tpId=13&tqId=11200&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
class Solution:
def Sum_Solution(self, n):
"""
借助 Python 的小 trick
"""
return sum(range(1, n + 1))
class Solution2:
"""
循环可以借助递归栈完成,那么问题就转移为如何判断递归是否应该结束
"""
def __init__(self):
self.result = 0
def Sum_Solution(self, n):
self.result += n
# 借助布尔运算的逻辑短路判断递归结束
n > 0 and self.Sum_Solution(n - 1)
return self.result
class Solution3:
def __init__(self):
self.result = 0
def Sum_Solution(self, n):
"""
借助异常停止递归
"""
try:
t = 1 // n
except ZeroDivisionError:
return self.result
self.result += n
self.Sum_Solution(n - 1)
return self.result
class Solution3:
"""
使用字典映射函数
"""
def __init__(self):
self.result = 0
self.index = 0
self.map = {True: self.recursion, False: self.stop}
def Sum_Solution(self, n):
self.result = 0
self.index = n
self.map[self.index > 0]()
return self.result
def recursion(self):
self.result += self.index
self.index -= 1
self.map[self.index > 0]()
def stop(self):
pass
import unittest
class TestCase(unittest.TestCase):
def setUp(self):
self.s = Solution3()
def test_1(self):
n = 1
r = self.s.Sum_Solution(n)
self.assertEqual(1, r)