zoukankan      html  css  js  c++  java
  • Leet Code OJ 简单(三)

    58.最后一个单词的长度  52ms

    class Solution:
        def lengthOfLastWord(self, s):
            """
            :type s: str
            :rtype: int
            """
            s = s.split(' ')
            while "" in s:
                s.remove("")
            if not s:
                return 0
            return len(s[-1])

    66.加一 56ms

    class Solution:
        def plusOne(self, digits):
            """
            :type digits: List[int]
            :rtype: List[int]
            """
            sum = 0
            r = []
            for index, i in enumerate(digits):
                sum += i*pow(10,len(digits)-index-1)
            sum += 1
            while sum:
                i = 1
                r.append(sum % pow(10,i))
                sum = sum // pow(10,i)
                i += 1
    
            r.reverse()
            return r

    67.二进制求和 60ms

    class Solution:
        def addBinary(self, a, b):
            """
            :type a: str
            :type b: str
            :rtype: str
            """
            return (bin(int(a, 2) + int(b, 2))[2:])

    69.x的平方根 76ms    击败了81.06% 的用户

    class Solution:
        def mySqrt(self, x):
            """
            :type x: int
            :rtype: int
            """
            return int(pow(x, 0.5))

    83.删除排序链表重复元素  76ms  击败了48.84% 的用户

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def deleteDuplicates(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head:
                return None
            cur = head
            while cur.next:
                if cur.val == cur.next.val:
                    if cur.next.next:
                        t = cur.next.next
                        cur.next = t
                    else:
                        cur.next = None
                else:
                    cur = cur.next
            return head
  • 相关阅读:
    2019.8.6原型链与继承
    2019.8.2闭包,作用域
    2019.8.1正则二
    2019.7.31正则
    2019.7.29二维数组
    2019.7.28关于数组和循环的八道题
    2019.7.27数组api
    DOM
    JavaScript数组5种去重方法
    JavaScript面向对象
  • 原文地址:https://www.cnblogs.com/FanMLei/p/10501002.html
Copyright © 2011-2022 走看看