└── Python ├── 012IntegerToRoman.py ├── 014LongestCommonPrefix.py ├── 015Sum3.py ├── 020ValidParentheses.py ├── 022GenerateParentheses.py ├── 028ImplementStrStr().py ├── 033SearchInRotatedSortedArray.py ├── 034SearchForARange.py ├── 050Pow(x,n).py ├── 070ClimbingStairs.py ├── 098ValidateBinarySearchTree.py └── 112PathSum.py /Python/012IntegerToRoman.py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/integer-to-roman/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/22869803 3 | # 1988lilong@163.com 4 | 5 | class Solution: 6 | # @return a string 7 | def intToRoman(self, num): 8 | base = [1000, 900, 500, 400, 100, 90, 50, 9 | 40, 10, 9, 5, 4, 1] 10 | roman = ["M", "CM", "D", "CD", "C", "XC", "L", 11 | "XL", "X", "IX", "V", "IV", "I"] 12 | 13 | result = "" 14 | 15 | i = 0 16 | while num > 0: 17 | count = num / base[i] 18 | num %= base[i] 19 | 20 | while count > 0: 21 | result += roman[i] 22 | count -= 1 23 | 24 | i += 1 25 | 26 | return result 27 | 28 | if __name__ == "__main__": 29 | slt = Solution() 30 | print slt.intToRoman(123) 31 | -------------------------------------------------------------------------------- /Python/014LongestCommonPrefix.py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/longest-common-prefix/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/22886331 3 | # 1988lilong@163.com 4 | 5 | class Solution: 6 | # @return a string 7 | def longestCommonPrefix(self, strs): 8 | if len(strs) == 0: 9 | return "" 10 | 11 | for i in range(len(strs[0])): 12 | for j in range(len(strs)): 13 | if len(strs[j]) <= i or strs[j][i] != strs[0][i]: 14 | return (strs[0])[0 : i] 15 | 16 | return strs[0] 17 | 18 | if __name__ == "__main__": 19 | strs = ["abc", "abcd", "ab"] 20 | 21 | slt = Solution() 22 | print slt.longestCommonPrefix(strs) 23 | -------------------------------------------------------------------------------- /Python/015Sum3.py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/3sum/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/21645437 3 | # 1988lilong@163.com 4 | 5 | class Solution: 6 | # @return a list of lists of length 3, [[val1,val2,val3]] 7 | def threeSum(self, num): 8 | num.sort() 9 | 10 | result = [] 11 | 12 | i = 0 13 | while i < len(num) - 2: 14 | if i > 0 and num[i] == num[i - 1]: 15 | i += 1 16 | continue 17 | 18 | j = i + 1 19 | k = len(num) - 1 20 | 21 | while j < k: 22 | if num[i] + num[j] + num[k] > 0: 23 | k -= 1 24 | elif num[i] + num[j] + num[k] < 0: 25 | j += 1 26 | else: 27 | tmp = [num[i], num[j], num[k]] 28 | result.append(tmp) 29 | 30 | j += 1 31 | k -= 1 32 | 33 | while j < k and num[j] == num[j - 1]: 34 | j += 1 35 | while j < k and num[k] == num[k + 1]: 36 | k -= 1 37 | i += 1 38 | 39 | return result 40 | 41 | if __name__ == "__main__": 42 | slt = Solution() 43 | num = [-1, 0, 1, 2, -1, -4] 44 | result = slt.threeSum(num) 45 | print result 46 | -------------------------------------------------------------------------------- /Python/020ValidParentheses.py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/valid-parentheses/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/21694751 3 | # 1988lilong@163.com 4 | 5 | class Solution: 6 | # @return a boolean 7 | def isValid(self, s): 8 | if len(s) == 0: 9 | return True 10 | 11 | st = [s[0]] 12 | 13 | i = 1 14 | while i < len(s): 15 | if len(st) == 0: 16 | st.append(s[i]) 17 | else: 18 | tmp = st.pop() 19 | if self.isMatch(tmp, s[i]): 20 | pass 21 | else: 22 | st.append(tmp) 23 | st.append(s[i]) 24 | 25 | i += 1 26 | 27 | if len(st) == 0: 28 | return True 29 | 30 | return False 31 | 32 | def isMatch(self, s, p): 33 | if (s == '(' and p == ')') or \ 34 | (s == '{' and p == '}') or \ 35 | (s == '[' and p == ']'): 36 | return True 37 | 38 | return False 39 | 40 | if __name__ == "__main__": 41 | slt = Solution() 42 | s = "()" 43 | print slt.isValid(s) 44 | -------------------------------------------------------------------------------- /Python/022GenerateParentheses.py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/generate-parentheses/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/23917967 3 | # 1988lilong@163.com 4 | 5 | class Solution: 6 | # @param an integer 7 | # @return a list of string 8 | def generateParenthesis(self, n): 9 | res = [] 10 | self.generate(n, n, "", res) 11 | return res 12 | 13 | def generate(self, left, right, str, res): 14 | if left == 0 and right == 0: 15 | res.append(str) 16 | return 17 | if left > 0: 18 | self.generate(left - 1, right, str + '(', res) 19 | if right > left: 20 | self.generate(left, right - 1, str + ')', res) 21 | 22 | if __name__ == "__main__": 23 | slt = Solution() 24 | print slt.generateParenthesis(3) 25 | -------------------------------------------------------------------------------- /Python/028ImplementStrStr().py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/implement-strstr/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/23655843 3 | # 1988lilong@163.com 4 | 5 | class Solution: 6 | # @param haystack, a string 7 | # @param needle, a string 8 | # @return a string or None 9 | def strStr(self, haystack, needle): 10 | if not needle: 11 | return haystack 12 | 13 | len1 = len(haystack) 14 | len2 = len(needle) 15 | if len1 < len2: 16 | return None 17 | 18 | for i in range(len1 - len2 + 1): 19 | j = 0 20 | k = i 21 | while j < len2 and needle[j] == haystack[k]: 22 | j += 1 23 | k += 1 24 | 25 | if j == len2: 26 | return haystack[i:] 27 | 28 | return None 29 | 30 | if __name__ == "__main__": 31 | slt = Solution() 32 | print slt.strStr("Hello", "ll") 33 | -------------------------------------------------------------------------------- /Python/033SearchInRotatedSortedArray.py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/search-in-rotated-sorted-array/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/22864861 3 | # 1988lilong@163.com 4 | 5 | class Solution: 6 | # @param A, a list of integers 7 | # @param target, an integer to be searched 8 | # @return an integer 9 | def search(self, A, target): 10 | left = 0 11 | right = len(A) - 1 12 | 13 | while left <= right: 14 | mid = (left + right) / 2 15 | 16 | if A[mid] == target: 17 | return mid 18 | 19 | if A[mid] >= A[left]: 20 | if A[mid] > target and A[left] <= target: 21 | right = mid - 1 22 | else: 23 | left = mid + 1 24 | else: 25 | if A[mid] < target and A[right] >= target: 26 | left = mid + 1 27 | else: 28 | right = mid - 1 29 | 30 | return -1 31 | 32 | if __name__ == "__main__": 33 | A = [4, 5, 6, 1, 2] 34 | 35 | slt = Solution() 36 | print slt.search(A, 5) 37 | print slt.search(A, 3) 38 | -------------------------------------------------------------------------------- /Python/034SearchForARange.py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/search-for-a-range/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/22893675 3 | # 1988lilong@163.com 4 | 5 | class Solution: 6 | # @param A, a list of integers 7 | # @param target, an integer to be searched 8 | # @return a list of length 2, [index1, index2] 9 | def searchRange(self, A, target): 10 | left = 0 11 | right = len(A) - 1 12 | 13 | result = [-1, -1] 14 | 15 | while left <= right: 16 | mid = (left + right) / 2 17 | 18 | if A[mid] > target: 19 | right = mid - 1 20 | elif A[mid] < target: 21 | left = mid + 1 22 | else: 23 | result[0] = mid 24 | result[1] = mid 25 | 26 | i = mid - 1 27 | while i >= 0 and A[i] == target: 28 | result[0] = i 29 | i -= 1 30 | 31 | i = mid + 1 32 | while i < len(A) and A[i] == target: 33 | result[1] = i 34 | i += 1 35 | 36 | break 37 | 38 | return result 39 | 40 | if __name__ == "__main__": 41 | A = [1, 2, 2, 4] 42 | 43 | slt = Solution() 44 | print slt.searchRange(A, 2) 45 | 46 | -------------------------------------------------------------------------------- /Python/050Pow(x,n).py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/powx-n/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/21701775 3 | # 1988lilong@163.com 4 | 5 | class Solution: 6 | # @param x, a float 7 | # @param n, a integer 8 | # @return a float 9 | def pow(self, x, n): 10 | if n < 0: 11 | return 1.0 / self.power(x, -n) 12 | else: 13 | return self.power(x, n) 14 | 15 | def power(self, x, n): 16 | if n == 0: 17 | return 1 18 | 19 | tmp = self.power(x, n / 2) 20 | 21 | if n & 0x01 == 1: 22 | return tmp * tmp * x 23 | else: 24 | return tmp * tmp 25 | 26 | if __name__ == "__main__": 27 | slt = Solution() 28 | print slt.pow(5, -1) 29 | -------------------------------------------------------------------------------- /Python/070ClimbingStairs.py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/climbing-stairs/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/21650907 3 | # 1988lilong@163.com 4 | 5 | class Solution: 6 | # @param n, an integer 7 | # @return an integer 8 | def climbStairs(self, n): 9 | a = [] 10 | a.append(1) 11 | a.append(1) 12 | 13 | for i in range(2, n + 1): 14 | a.append(a[i - 1] + a[i - 2]) 15 | 16 | return a[n] 17 | 18 | if __name__ == "__main__": 19 | slt = Solution() 20 | result = slt.climbStairs(3) 21 | print result 22 | -------------------------------------------------------------------------------- /Python/098ValidateBinarySearchTree.py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/validate-binary-search-tree/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/22780563 3 | # 1988lilong@163.com 4 | 5 | class TreeNode: 6 | def __init__(self, x): 7 | self.val = x 8 | self.left = None 9 | self.right = None 10 | 11 | class Solution: 12 | # @param root, a tree node 13 | # @return a boolean 14 | def isValidBST(self, root): 15 | return self.isValidBSTImpl(root, -2**31, 2**31 - 1) 16 | 17 | def isValidBSTImpl(self, root, min, max): 18 | if root == None: 19 | return True 20 | 21 | return root.val > min and root.val < max \ 22 | and self.isValidBSTImpl(root.left, min, root.val) \ 23 | and self.isValidBSTImpl(root.right, root.val, max) 24 | 25 | if __name__ == "__main__": 26 | n1 = TreeNode(0) 27 | n2 = TreeNode(2) 28 | n3 = TreeNode(3) 29 | n1.left = n2 30 | n1.right = n3 31 | 32 | slt = Solution() 33 | print slt.isValidBST(n1) 34 | -------------------------------------------------------------------------------- /Python/112PathSum.py: -------------------------------------------------------------------------------- 1 | # Problem: http://oj.leetcode.com/problems/path-sum/ 2 | # Analysis: http://blog.csdn.net/lilong_dream/article/details/22875143 3 | # 1988lilong@163.com 4 | 5 | class TreeNode: 6 | def __init__(self, x): 7 | self.val = x 8 | self.left = None 9 | self.right = None 10 | 11 | class Solution: 12 | # @param root, a tree node 13 | # @param sum, an integer 14 | # @return a boolean 15 | def hasPathSum(self, root, sum): 16 | if root == None: 17 | return False 18 | 19 | if root.left == None and root.right == None: 20 | return root.val == sum 21 | 22 | return self.hasPathSum(root.left, sum - root.val) \ 23 | or self.hasPathSum(root.right, sum - root.val) 24 | 25 | if __name__ == "__main__": 26 | # 5 27 | # / \ 28 | # 4 8 29 | # / / \ 30 | # 11 13 4 31 | # / \ \ 32 | # 7 2 1 33 | n1 = TreeNode(5) 34 | n2 = TreeNode(4) 35 | n3 = TreeNode(8) 36 | n4 = TreeNode(11) 37 | n5 = TreeNode(13) 38 | n6 = TreeNode(4) 39 | n7 = TreeNode(7) 40 | n8 = TreeNode(2) 41 | n9 = TreeNode(1) 42 | n1.left = n2 43 | n1.right = n3 44 | n2.left = n4 45 | n3.left = n5 46 | n3.right = n6 47 | n4.left = n7 48 | n4.right = n8 49 | n6.right = n9 50 | 51 | slt = Solution() 52 | print slt.hasPathSum(n1, 22) 53 | --------------------------------------------------------------------------------