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

    20. 有效括号  48ms

    class Solution:
        def isValid(self, s):
            """
            :type s: str
            :rtype: bool
            """
            if len(s) % 2 :
                return False
            brackets = {'(': ')', '{': '}', '[': ']'}
            stack = []
            for i in s:
                if i in brackets:
                    stack.append(i)
                else:
                    if not stack or brackets[stack.pop()] != i:
                        return False
            if stack:
                return False
            return True

    26.删除排序数组中的重复项  96ms

    class Solution:
        def removeDuplicates(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if len(nums) <= 1:
                return len(nums)
            s = 0
            for f in range(1, len(nums)):
                if nums[s] != nums[f]:
                    s += 1
                    nums[s] = nums[f]
            return s + 1

    27.移除元素  56ms

    class Solution:
        def removeElement(self, nums, val):
            """
            :type nums: List[int]
            :type val: int
            :rtype: int
            """
            if val not in nums:
                return len(nums)
            while val in nums:
                nums.remove(val)
            return len(nums)

    28.实现strStr()  48ms

    class Solution:
        def strStr(self, haystack, needle):
            """
            :type haystack: str
            :type needle: str
            :rtype: int
            """
            if not needle:
                return 0
            if needle not in haystack:
                return -1
            else:
                return haystack.index(needle)

    35.搜索插入位置  48ms

    class Solution:
        def searchInsert(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: int
            """
            if target in nums:
                return nums.index(target)
            if target < nums[0]:
                return 0
            if target > nums[-1]:
                return len(nums)
            for i in range(len(nums)-1):
                if nums[i]<target and nums[i+1]>target:
                    return i+1
  • 相关阅读:
    CSS 背景
    CSS padding 属性
    CSS border 属性和 border-collapse 属性
    CSS margin 属性
    IEnumerable<T> 接口和GetEnumerator 详解
    discuz! X3.4特殊字符乱码解决方案
    Discuz通过修改文章标题更好的实现SEO的方法
    关于Discuz x3.3页面空白解决方法
    discuz x3.3标题的最少字数限制设置方法
    discuz网站前端代码优化思路
  • 原文地址:https://www.cnblogs.com/FanMLei/p/10501003.html
Copyright © 2011-2022 走看看