leetcode刷题
Posted ruanshuai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode刷题相关的知识,希望对你有一定的参考价值。
1 两数之和
#20181205
class Solution: def twoSum(self,nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ n = len(nums) d = {} for i in range(n): a = target - nums[i] if nums[i] in d: return d[nums[i]], i else: d[a] = i
2 整数反转
#20181206
class Solution: def reverse(self, x): """ :type x: int :rtype: int """ reverse_x = "" sign = "" if x < 0: x = -x sign = "-" for i in str(x): reverse_x = i + reverse_x reverse_x = sign + reverse_x if int(reverse_x) < pow(-2, 31) or int(reverse_x) > pow(2, 31) - 1: return 0 return int(reverse_x)
以上是关于leetcode刷题的主要内容,如果未能解决你的问题,请参考以下文章