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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| class Solution2(object): def addOperators(self, num, target): """ 这个居然通过了。呵呵。不过是勉强通过的。 :type num: str :type target: int :rtype: List[str] """ if num: end = len(num) result = [] path = [num[0]] s1 = [int(num[0])] s2 = [] begin = 1 self.find(begin, end, num, s1, s2, path, target, result) return result else: return []
def find(self, begin, end, num, s1, s2, path, target, result): if begin == end: if s1[0] == target: result.append("".join(path)) else: ops = [("+", 10), ("-", 10), ("*", 20), (".", 30)] ps = ["+", "-", "*", ""] if begin + 1 < end: for i in range(4): s11 = s1[:] s22 = s2[:] path.append(ps[i] + num[begin]) self.helper(begin, end, num, ops[i], path, result, s11, s22, target) path.pop() elif begin + 1 == end: for i in range(4): s11 = s1[:] s22 = s2[:] path.append(ps[i] + num[begin]) self.helper2(begin, end, num, ops[i], path, result, s11, s22, target) path.pop()
def helper(self, begin, end, num, op, path, result, s1, s2, target): if s2: op_pre = s2[-1] if op_pre[1] < op[1]: s1.append(int(num[begin])) s2.append(op) self.find(begin + 1, end, num, s1, s2, path, target, result)
else: is_valid = True while s2: op_pre = s2[-1] if op_pre[1] < op[1]: break op_pre = s2.pop() n2 = s1.pop() n1 = s1[-1] if op_pre[0] == "+": s1[-1] = n1 + n2 elif op_pre[0] == "-": s1[-1] = n1 - n2 elif op_pre[0] == "*": s1[-1] = n1 * n2 elif op_pre[0] == "." and n1 != 0: s1[-1] = n1 * 10 + n2 else: is_valid = False break if is_valid: s1.append(int(num[begin])) s2.append(op) self.find(begin + 1, end, num, s1, s2, path, target, result) else: s1.append(int(num[begin])) s2.append(op) self.find(begin + 1, end, num, s1, s2, path, target, result)
def helper2(self, begin, end, num, op, path, result, s1, s2, target): if s2: op_pre = s2[-1] if op_pre[1] < op[1]: s1.append(int(num[begin])) s2.append(op)
is_valid = True while s2: op_pre = s2.pop() n2 = s1.pop() n1 = s1[-1] if op_pre[0] == "+": s1[-1] = n1 + n2 elif op_pre[0] == "-": s1[-1] = n1 - n2 elif op_pre[0] == "*": s1[-1] = n1 * n2 elif op_pre[0] == "." and n1 != 0: s1[-1] = n1 * 10 + n2 else: is_valid = False break if is_valid: self.find(begin + 1, end, num, s1, s2, path, target, result) else: is_valid = True while s2: op_pre = s2[-1] if op_pre[1] < op[1]: break op_pre = s2.pop() n2 = s1.pop() n1 = s1[-1] if op_pre[0] == "+": s1[-1] = n1 + n2 elif op_pre[0] == "-": s1[-1] = n1 - n2 elif op_pre[0] == "*": s1[-1] = n1 * n2 elif op_pre[0] == "." and n1 != 0: s1[-1] = n1 * 10 + n2 else: is_valid = False break
if is_valid: s2.append(op) s1.append(int(num[begin])) while s2: op_pre = s2.pop() n2 = s1.pop() n1 = s1[-1] if op_pre[0] == "+": s1[-1] = n1 + n2 elif op_pre[0] == "-": s1[-1] = n1 - n2 elif op_pre[0] == "*": s1[-1] = n1 * n2 elif op_pre[0] == "." and n1 != 0: s1[-1] = n1 * 10 + n2 else: is_valid = False break
if is_valid: self.find(begin + 1, end, num, s1, s2, path, target, result) else: n2 = int(num[begin]) n1 = s1[-1] is_valid = True if op[0] == "+": s1[-1] = n1 + n2 elif op[0] == "-": s1[-1] = n1 - n2 elif op[0] == "*": s1[-1] = n1 * n2 elif op[0] == "." and n1 != 0: s1[-1] = n1 * 10 + n2 else: is_valid = False
if is_valid: self.find(begin + 1, end, num, s1, s2, path, target, result)
|