![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leecode
Mryang2333
人生苦短,我用Python。
展开
-
7.整数反转(通过)Python
7.整数反转(通过)给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转class Solution: def reverse(self, x): """ :type x: int :rtype: int """ y = abs(x) l = list(str(y)) ..原创 2018-12-24 10:18:52 · 636 阅读 · 0 评论 -
69.X的平方根(借鉴)Python
首先我想到的是直接return x**0.5但是人家明显不让这么做。。。然后借鉴。。。牛顿迭代法实现。class Solution: def mySqrt(self, x): """ :type x: int :rtype: int """ if x <= 1: retur...原创 2018-12-25 19:41:24 · 334 阅读 · 0 评论 -
70.爬楼梯(通过)Python
前9阶的解法分别是:1,2,3,5,8,13,21,34,55由上可知,与Fibonacci sequence及其相似class Solution: def climbStairs(self, n): """ :type n: int :rtype: int """ tmp = 1 res ...原创 2018-12-25 20:10:28 · 284 阅读 · 0 评论 -
83.删除排序链表中的重复元素(通过)Python
和c有点不一样,Python不需要释放被删除的节点# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def deleteDuplicate...原创 2018-12-26 10:17:25 · 159 阅读 · 0 评论 -
100.相同的树(通过)Python
上学那会写的最多的就是前中后层遍历,各种转换。。。# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solut...原创 2019-01-02 16:04:02 · 184 阅读 · 0 评论 -
101.对称二叉树(通过)Python
用的递归在第100题的基础上改一改就可以了# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution...原创 2019-01-02 17:06:18 · 174 阅读 · 0 评论 -
104.二叉树的最大深度(通过)Python
二叉树的最大深度# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def maxD...原创 2019-01-02 17:40:15 · 443 阅读 · 0 评论 -
171.Excel表序列号(通过)Python
class Solution: def titleToNumber(self, s): """ :type s: str :rtype: int """ num = 0 for i in range(len(s)-1,-1,-1): num += (ord(s[i]) -...原创 2019-01-28 08:48:49 · 287 阅读 · 0 评论 -
344.反转字符串(通过)Python
最开始想用s.reverse()后来还是写了个替换class Solution: def reverseString(self, s): """ :type s: str :rtype: str """ l = len(s) - 1 for i in range(l): ..原创 2019-01-28 09:24:45 · 223 阅读 · 0 评论 -
461.汉明距离(通过)Python
class Solution: def hammingDistance(self, x, y): """ :type x: int :type y: int :rtype: int """ count = 0 while True: if x == 0 a...原创 2019-01-28 09:26:04 · 978 阅读 · 0 评论 -
67.二进制求和(通过)Python
二进制求和最蠢写法(没有用bin()和int())后边附上使用int和bin版本class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ a = list(map(int,l...原创 2018-12-25 17:17:37 · 262 阅读 · 0 评论 -
66.加一(通过)Python2/3
map()函数Python 2.x 返回列表Python 3.x 返回迭代器。python2class Solution(object): def plusOne(self, digits): """ :type digits: List[int] :rtype: List[int] """ digit...原创 2018-12-25 15:31:16 · 171 阅读 · 0 评论 -
9.回文数(通过)Python
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。class Solution: def isPalindrome(self, x): &quot;&quot;&quot; :type x: int :rtype: bool &quot;&quot;&quot; y = list(str原创 2018-12-24 10:58:52 · 135 阅读 · 0 评论 -
13.罗马数字转整数(通过)Python
class Solution: def romanToInt(self, s): &quot;&quot;&quot; :type s: str :rtype: int &quot;&quot;&quot; y = list(s) vl = {'I':1,'V':5,'X':10,'L':50,'C':100,原创 2018-12-24 12:01:01 · 146 阅读 · 0 评论 -
20.有效的括号(通过)Python
给定一个只包括 {} 的字符串,判断字符串是否有效。class Solution: def isValid(self, s): """ :type s: str :rtype: bool """ vl = {'{':1,'}':-1,'[':2,']':-2,'(':3,')':-3} y ..原创 2018-12-24 18:50:52 · 217 阅读 · 0 评论 -
26.删除排序数组中的重复项(勉强通过)Python
emmm…这个答案有问题,太慢了,不让用set(),难受class Solution: def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ count = 0 for i in range(le..原创 2018-12-24 20:27:16 · 129 阅读 · 0 评论 -
27.移除元素(通过)Python
class Solution: def removeElement(self, nums, val): &quot;&quot;&quot; :type nums: List[int] :type val: int :rtype: int &quot;&quot;&quot; count = nums.coun原创 2018-12-24 20:38:33 · 185 阅读 · 2 评论 -
28.实现strStr()Python
偷懒写法,emmm看了看题名,想了一下,这应该是让我实现Python的index()吧 def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ try: hay..原创 2018-12-24 20:55:32 · 209 阅读 · 0 评论 -
35.搜索插入位置(通过)Python
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。class Solution: def searchInsert(self, nums, target): """ :type nums: List[int] :type target: in...原创 2018-12-25 08:43:36 · 198 阅读 · 0 评论 -
53.最大子序和(通过)Python
class Solution: def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ sum = 0 Maxsum = nums[0] for i in range(len(nums)):...原创 2018-12-25 10:58:49 · 256 阅读 · 0 评论 -
58.最后一个单词的长度(通过)Python
给定一个仅包含大小写字母和空格’ ’ 的字符串,返回其最后一个单词的长度。如果不存在最后一个单词,请返回 0说明:一个单词是指由字母组成,但不包含任何空格的字符串。class Solution: def lengthOfLastWord(self, s): """ :type s: str :rtype: int ""...原创 2018-12-25 14:22:43 · 299 阅读 · 0 评论 -
561.数组拆分1(通过)Python
class Solution: def arrayPairSum(self, nums): """ :type nums: List[int] :rtype: int """ listNum = list(nums) listNum.sort() sum = 0 ...原创 2019-01-28 09:27:26 · 248 阅读 · 0 评论