├── LICENSE ├── README.md ├── leetcode-009 └── leetcode009.py ├── leetcode-013 └── leetcode013.py ├── leetcode-058 └── leetcode058.py ├── leetcode-067 └── leetcode67.py ├── leetcode-089 └── leetcode89.py ├── leetcode-136 ├── leetcode136-method1.py └── solution-method2.py ├── leetcode-202 └── leetcode202.py ├── leetcode-204 └── leetcode204-method1.py ├── leetcode-345 └── leetcode345.py ├── leetcode-645 └── leetcode645.py ├── leetcode-771 └── leetcode771.py ├── leetcode-976 └── leetcode976.py └── leetcode-977 └── leetcode977.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Python Tech 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python玩转LeetCode 2 | Python 玩转 LeetCode 的学习之路 3 | 4 | - [Python 玩转 LeetCode 面试题](http://www.justdopython.com/category/#/Leetcode面试题) 5 | 6 | - [Leetcode 面试系列 第1天:Leetcode 89 - 格雷码](http://www.justdopython.com/2019/09/09/python-leetcode89-gary-code/) 7 | 8 | - [LeetCode 面试系列 第2天:No.136 - 只出现一次的数](http://www.justdopython.com/2019/09/13/python-leetcode136-single-number/) 9 | 10 | - [LeetCode 面试系列 第3天:No.67 - 二进制数求和](http://www.justdopython.com/2019/09/14/python-leetcode67-add-binary/) 11 | 12 | - [LeetCode 面试系列 第4天:No.202 - 快乐数](http://www.justdopython.com/2019/09/28/python-leetcode202-happy-number/) 13 | 14 | - [LeetCode 面试系列 第5天:No.204 - 统计质数](http://www.justdopython.com/2019/10/04/python-leetcode204-count-primes/) 15 | 16 | - [LeetCode面试系列 第6天:No.9 - 回文数](http://www.justdopython.com/2019/10/19/python-leetcode9-palindrome-number/) 17 | 18 | - [LeetCode面试系列 第7天:No.13 - 罗马数字转整数](http://www.justdopython.com/2019/10/26/python-leetcode13-roman-to-integer/) 19 | 20 | - [LeetCode面试系列 第8天:No.58 - 最后一个单词的长度](http://www.justdopython.com/2019/10/30/python-leetcode58-length-of-last-word/) 21 | 22 | - [LeetCode面试系列 第9天:No.345 - 反转字符串中的元音字母](http://www.justdopython.com/2019/11/13/leetcode345-reverse-vowels-of-a-string/) 23 | 24 | - [LeetCode面试系列 第10天:No.976 - 三角形的最大周长](http://www.justdopython.com/2019/11/20/leetcode976-largest-perimeter-triangle/) 25 | 26 | - [LeetCode面试系列 第12天:No.977 - 有序数组的平方](http://www.justdopython.com/2019/11/25/python-leetcode977-squares-of-a-sorted-array/) 27 | 28 | - [LeetCode面试系列 第11天:No.645 - 错误的集合](http://www.justdopython.com/2019/11/25/python-leetcode645-set-mismatch/) 29 | 30 | 31 | 关注公众号:python技术,回复"python"一起学习交流 32 | 33 | ![gzh](http://www.justdopython.com/assets/images/wechat-qcode.jpg) 34 | -------------------------------------------------------------------------------- /leetcode-009/leetcode009.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, x: int) -> bool: 3 | if x < 0: 4 | return False 5 | else: 6 | str0 = str(x) 7 | reversedStr = str0[::-1] 8 | if reversedStr == str0: 9 | return True 10 | return False 11 | 12 | # Below is testing 13 | sol = Solution() 14 | print(sol.isPalindrome(616)) -------------------------------------------------------------------------------- /leetcode-013/leetcode013.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def romanToInt(self, s: str) -> int: 3 | d = dict() 4 | d = { 5 | 'I': 1, 6 | 'V': 5, 7 | 'X': 10, 8 | 'L': 50, 9 | 'C': 100, 10 | 'D': 500, 11 | 'M': 1000 12 | } 13 | 14 | sum0 = 0 15 | for i in range(len(s)): 16 | currentValue = d[s[i]] 17 | 18 | if i == len(s) -1 or d[s[i+1]] <= currentValue: # d[s[i+1]]: nextValue 19 | sum0 += d[s[i]] 20 | else: 21 | sum0 -= d[s[i]] 22 | 23 | return sum0 24 | 25 | sol = Solution() 26 | print(sol.romanToInt("MCMXCIV")) -------------------------------------------------------------------------------- /leetcode-058/leetcode058.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLastWord(self, s: str) -> int: 3 | newStr = s.strip() 4 | lastSpacePos = newStr.rfind(' ') 5 | if lastSpacePos == -1: 6 | return len(newStr) 7 | else: 8 | return len(newStr) - lastSpacePos - 1 9 | 10 | sol = Solution() 11 | print(sol.lengthOfLastWord('Hello my baby')) -------------------------------------------------------------------------------- /leetcode-067/leetcode67.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addBinary(self, a: str, b: str) -> str: 3 | num1 = int(a, 2) 4 | num2 = int(b, 2) 5 | sum0 = num1 + num2 6 | return format(sum0, 'b') 7 | 8 | # 测试 9 | sol = Solution() 10 | print(sol.addBinary('11', '1101')) -------------------------------------------------------------------------------- /leetcode-089/leetcode89.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | class Solution: 4 | def grayCode(self, n: int) -> List[int]: 5 | res = [] 6 | for i in range(1 << n): 7 | res.append((i >> 1) ^ i) 8 | return res 9 | 10 | # Below is testing 11 | obj = Solution() 12 | print(obj.grayCode(2)) -------------------------------------------------------------------------------- /leetcode-136/leetcode136-method1.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | class Solution: 4 | def singleNumber(self, nums: List[int]) -> int: 5 | res = 0 6 | d = dict() 7 | for num in nums: 8 | if num not in d: 9 | d[num] = 1 10 | else: 11 | d[num] += 1 12 | res = next(k for k, val in d.items() if val == 1) 13 | return res 14 | 15 | #以下是测试 16 | sol = Solution() 17 | print(sol.singleNumber([6, 4, 6, 2, 4])) -------------------------------------------------------------------------------- /leetcode-136/solution-method2.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | class Solution: 4 | def singleNumber(self, nums: List[int]) -> int: 5 | r = 0 6 | for i in nums: 7 | r ^= i 8 | return r 9 | 10 | #以下是测试 11 | sol = Solution() 12 | print(sol.singleNumber([6, 4, 6, 2, 4])) -------------------------------------------------------------------------------- /leetcode-202/leetcode202.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isHappy(self, n: int) -> bool: 3 | unhappy = set() 4 | while n not in unhappy and n != 1: 5 | unhappy.add(n) 6 | n = self.GetSquareSum(n) 7 | return n == 1 8 | 9 | def GetSquareSum(self, n: int) -> bool: 10 | sum0 = 0 11 | while n > 0: 12 | r = n - int(n/10)*10 13 | n = int(n/10) 14 | sum0 += r * r 15 | return sum0 16 | 17 | sol = Solution() 18 | print(sol.isHappy(19)) -------------------------------------------------------------------------------- /leetcode-204/leetcode204-method1.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JustDoPython/leetcode-python/ad198f215bdb6c3266341bc440cf39c170ff8e5e/leetcode-204/leetcode204-method1.py -------------------------------------------------------------------------------- /leetcode-345/leetcode345.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseVowels(self, s: str) -> str: 3 | vowels = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'] 4 | vList = list() 5 | 6 | vCount = 0 7 | for index in range(len(s)): 8 | if s[index] in vowels: 9 | vList.append(s[index]) 10 | 11 | res_str = '' 12 | vList.reverse() 13 | 14 | vOutCount = 0 15 | for index in range(len(s)): 16 | if s[index] not in vowels: 17 | res_str += s[index] 18 | else: 19 | res_str += vList[vOutCount] 20 | vOutCount += 1 21 | 22 | return res_str 23 | 24 | sol = Solution() 25 | print(sol.reverseVowels("hiHeyBaby")) -------------------------------------------------------------------------------- /leetcode-645/leetcode645.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | class Solution: 4 | def findErrorNums(self, nums: List[int]) -> List[int]: 5 | res = list() 6 | nums.sort() 7 | stdList = range(1, len(nums) + 1) # len(nums) = n 8 | repeatedNum = sum(nums) - sum(set(nums)) 9 | res.append(repeatedNum) 10 | missingNum = (set(stdList) - set(nums)).pop() 11 | res.append(missingNum) 12 | return res 13 | 14 | # below is testing 15 | sol = Solution() 16 | print(sol.findErrorNums([4,2, 1,2])) 17 | -------------------------------------------------------------------------------- /leetcode-771/leetcode771.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numJewelsInStones(self, J: str, S: str) -> int: 3 | sum0 = 0 4 | 5 | for j in J: 6 | sum0 = sum0 + S.count(j) 7 | return sum0 8 | 9 | # Below is tesing 10 | sol = Solution() 11 | print(sol.numJewelsInStones('aA', 'aAAbbbb')) -------------------------------------------------------------------------------- /leetcode-976/leetcode976.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | class Solution: 3 | def largestPerimeter(self, A: List[int]) -> int: 4 | if (len(A) < 3): 5 | return 0 6 | 7 | A.sort(reverse=True) 8 | for i in range(len(A)-2): 9 | if A[i] < A[i+1] + A[i+2]: # Can build a triangle 10 | return A[i] + A[i+1] + A[i+2] 11 | return 0 12 | 13 | # Below is testing 14 | sol = Solution() 15 | print(sol.largestPerimeter([4, 5, 2, 7])) -------------------------------------------------------------------------------- /leetcode-977/leetcode977.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | class Solution: 4 | def sortedSquares(self, A: List[int]) -> List[int]: 5 | B = sorted(A, key = abs) # sort by absolute values 6 | res = list() 7 | for elem in B: 8 | res.append(elem*elem) 9 | return res 10 | 11 | # below is testing 12 | sol = Solution() 13 | print(sol.sortedSquares([-4,-2, 3,6])) --------------------------------------------------------------------------------