摘要: class Solution: def isPalindrome(self, x: int) -> bool: # 参考 7.整数反转 num = 0 a = abs(x) while(a != 0): temp = a % 10 num = num*10 + temp a = a//10 if x >= 0 and x == num: return True else: return False 阅读全文
posted @ 2019-08-21 22:13 我叫郑小白 阅读(85) 评论(0) 推荐(0) 编辑
摘要: class Solution: def reverse(self, x: int) -> int: num = 0 # 取绝对值 a = abs(x) while(a != 0): # 首先,假设 a = 123 # num = 0 # a = 12 # num = 3 # a = 1 # num = 32 # a = 0 # num = 321 # a=0时,结束循环 temp = a % 10 阅读全文
posted @ 2019-08-21 22:04 我叫郑小白 阅读(103) 评论(0) 推荐(0) 编辑
摘要: 方法一: 方法二: 阅读全文
posted @ 2019-08-21 15:35 我叫郑小白 阅读(137) 评论(0) 推荐(0) 编辑
摘要: class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: # 创建一个初始链表 curr = dummy = ListNode(0) # 遍历两个链表 while l1 and l2: ... 阅读全文
posted @ 2019-08-21 11:13 我叫郑小白 阅读(137) 评论(0) 推荐(0) 编辑
摘要: class Solution: def removeElement(self, nums: List[int], val: int) -> int: # 定义两个指针,一个头,一个尾 i, last = 0, len(nums) - 1 while i <= last: # 判断头指针当前对应的列表中的值是否与val相等 # 若是,则头指针和尾指针对应的数进行调换 if nums[i] == va 阅读全文
posted @ 2019-08-21 10:00 我叫郑小白 阅读(103) 评论(0) 推荐(0) 编辑
摘要: class Solution: def strStr(self, haystack: str, needle: str) -> int: for i in range(len(haystack) - len(needle) + 1): # 判断needle是否属于haystack中的一部分 if haystack[i:i+l... 阅读全文
posted @ 2019-08-20 18:38 我叫郑小白 阅读(85) 评论(0) 推荐(0) 编辑
摘要: class Solution: def isValid(self, s: str) -> bool: stack = [] lookup = {'(':')', '[':']', '{':'}'} for parenthese in s: # 如果是左括号,就加入到stack中 if pare... 阅读全文
posted @ 2019-08-20 18:20 我叫郑小白 阅读(142) 评论(0) 推荐(0) 编辑
摘要: class Solution: def removeDuplicates(self, nums: List[int]) -> int: if not nums: return 0 count = 0 for i in range(len(nums)): if nums[count] != nums[i]: count += 1 nums[count]= nums[i] # count从0开始的,如 阅读全文
posted @ 2019-08-20 17:51 我叫郑小白 阅读(106) 评论(0) 推荐(0) 编辑
摘要: class Solution: def generateParenthesis(self, n: int) -> List[str]: if n == 0 : return [] result = [] # 递归法 self.helper(n, n, '', result) return result def helper(self, left, right, item, result): # 如 阅读全文
posted @ 2019-08-20 17:22 我叫郑小白 阅读(139) 评论(0) 推荐(0) 编辑
摘要: class Solution: def romanToInt(self, s: str) -> int: # 将各个字母表示的数字存入字典中 numeral_map = {"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000} # 存储罗马字母转成的整数值 result = 0... 阅读全文
posted @ 2019-08-14 20:57 我叫郑小白 阅读(146) 评论(0) 推荐(0) 编辑