Leet Code OJ 简单(二)
生活随笔
收集整理的這篇文章主要介紹了
Leet Code OJ 简单(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
20.?有效括號? 48ms
class Solution:def isValid(self, s):""":type s: str:rtype: bool"""if len(s) % 2 :return Falsebrackets = {'(': ')', '{': '}', '[': ']'}stack = []for i in s:if i in brackets:stack.append(i)else:if not stack or brackets[stack.pop()] != i:return Falseif stack:return Falsereturn True26.刪除排序數組中的重復項? 96ms
class Solution:def removeDuplicates(self, nums):""":type nums: List[int]:rtype: int"""if len(nums) <= 1:return len(nums)s = 0for f in range(1, len(nums)):if nums[s] != nums[f]:s += 1nums[s] = nums[f]return s + 127.移除元素? 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 0if needle not in haystack:return -1else: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 0if 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?
轉載于:https://www.cnblogs.com/FanMLei/p/10501003.html
總結
以上是生活随笔為你收集整理的Leet Code OJ 简单(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL学习笔记(六)MySQL8.0
- 下一篇: 百度AI智能学习