leetcode题目及答案python_leetcode题库解答源码(python3)

下面和大家分享本人在leetcode上已经ace的题目源码(python3): 本人会持续更新!~

class Leetcode_Solution(object):

def twoSum_1(self,nums, target):

"""

:type nums: List[int]

:type target: int

:rtype: List[int]

"""

'''

# 此解法复杂度为O(n^2)

new_nums = []

for i in range(len(nums)):

for j in range(i+1,len(nums)):

if nums[i] + nums[j] == target:

new_nums.append(i)

new_nums.append(j)

return new_nums

'''

# 此解法复杂度为O(n)

# 拓展:若解不唯一,可先将nums排序后进行下面操作,将全部符合对输出

if len(nums)<= 1:

return False

else:

dict = {}

for i in range(len(nums)):

# 字典底层是用hash表实现的,无论字典中有多少元素,查找的平云复杂度均为O(1)

if num[i] in dict:

return [dict[nums[i]], i]

else:

dict[target - nums[i]] = i

def reverse_7(self,x):

"""

:type x: int

:rtype: int

"""

MAX = 2**31 - 1

min = -1*2**31

if x < 0:

y = -1*int(str(-x)[::-1])

else:

y = int(str(x)[::-1])

if y > Max or y < min:

return 0

return y

def isPalindrome_9(self, x):

renum = 0

if x < 0 or (x % 10 == 0 and x != 0):

return False

while x > renum:

renum = renum * 10 + x % 10

x /= 10

return x == renum or x == renum/10

def romanToInt_13(self, s):

"""

:type s: str

:rtype: int

"""

dic = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}

sum = 0

for i in range(len(s)-1):

if dic[s[i]] < dic[s[i+1]]:

sum -= dic[s[i]]

else:

sum += dic[s[i]]

return sum + dic[s[-1]]

def longestCommonPrefix_14(self, strs):

"""

:type strs: List[str]

:rtype: str

"""

if len(strs) == 0: # Horizontal scanning/another way: vertical scanning

return ''

prefix = strs[0]

for i in range(1,len(strs)):

while strs[i].find(prefix) != 0:

prefix = prefix[0:len(prefix)-1]

if prefix == '':

return ''

return prefix

def isValid_20(self, s):

"""

:type s: str

:rtype: bool

"""

'''

list = []

a = b = c = 0

if len(s) == 0:

return True

for i in range(len(s)):

if s[i] == '(':

list.append(s[i])

a += 1

if s[i] == '{':

list.append(s[i])

b += 1

if s[i] == '[':

list.append(s[i])

c += 1

if s[i] == ')':

if len(list) != 0 and list[-1] == '(':

list.pop()

a -= 1

else:

return False

if s[i] == '}':

if len(list) != 0 and list[-1] == '{':

list.pop()

b -= 1

else:

return False

if s[i] == ']':

if len(list) != 0 and list[-1] == '[':

list.pop()

c -= 1

else:

return False

if len(list) == 0 and a == b == c == 0:

return True

else:

return False

'''

dic = {')':'(','{':'}','[':']'}

stack = []

for i in s:

if i in dic.values():

stack.append(i)

elif i in dic.keys():

if stack == [] or dic[i] != stack.pop():

return False

else:

return False

return stack == []

def mergeTwoLists_21(self, l1, l2):

"""

:type l1: ListNode

:type l2: ListNode

:rtype: ListNode

"""

# Definition for singly-linked list.

# class ListNode:

# def __init__(self, x):

# self.val = x

# self.next = None

head = rear = ListNode(0)

while l1 and l2:

if l1.val < l2.val:

rear.next = l1

l1 = l1.next

else:

rear.next = l2

l2 = l2.next

rear = rear.next

rear.next = l1 or l2

return head.next

def removeDuplicates_26(self, nums):

"""

:type nums: List[int]

:rtype: int

"""

if len(nums) == 0:

return 0

newtail = 0

for i in range(1,len(nums)):

if nums[i] != nums[newtail]:

newtail += 1

nums[newtail] = nums[i]

return newtail + 1

def removeElement_27(self, nums, val):

"""

:type nums: List[int]

:type val: int

:rtype: int

"""

i = len(nums)

j = 0

if i == 0:

return 0

while j < i:

if nums[j] == val:

nums.pop(j)

i -= 1

else:

j += 1

return len(nums)

def strStr_28(self, haystack, needle):

"""

:type haystack: str

:type needle: str

:rtype: int

"""

for i in range(len(haystack) - len(needle) +1):

if haystack[i:i+len(needle)] == needle:

return i

return -1

def searchInsert_35(self, nums, target):

"""

:type nums: List[int]

:type target: int

:rtype: int

"""

return len([x for x in nums if x < target])

def countAndSay(self, n):

"""

:type n: int

:rtype: str

"""

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
魔众题库系统基于PHP开发,可以用于题库管理和试卷生成软件,拥有极简界面和强大的功能,用户遍及全国各行各业。 魔众题库系统,融合在线题库和在线考试。 2022年10月15日魔众题库系统发布v7.6.0版本,增加了以下34个特性: ·[新功能] 系统升级调用命令容错处理 ·[新功能] 用户注册IP信息 ·[新功能] TextAjaxRequest组件部分属性重构和新增方法 ·[新功能] 后台登录背景优化 ·[新功能] 富文本标签A新增download属性过滤 ·[新功能] 用户注册登录弹窗逻辑兼容处理 ·[新功能] 模块新增标签属性功能 ·[新功能] ProviderTrait新增是否排序自动检测 ·[新功能] 数据库兼容MySQL8.0 ·[新功能] OpenApi和Api中间件新增AccessGate ·[新功能] 文件上传表新增大类和分类索引 ·[新功能] Request新增isGet方法用于判断GET请求方式 ·[新功能] 用户新增Meta信息,用于底层临时存储部分关联信息 ·[新功能] 用户注册处理器新增排序字段 ·[新功能] 网站地址配置项 ·[新功能] 用户钱包充值接口功能 ·[新功能] 手机快捷注册自动设置用户名和昵称 ·[新功能] 富文本过滤图片新增data-formula-image属性 ·[新功能] 用户VIP开通赠送积分功能开启 ·[新功能] 授权登录绑定手机和邮箱可配置 ·[新功能] 后台新增重新触发事件操作 ·[新功能] 订单新增支付事件是否成功状态 ·[新功能] 随机字符串新增大写和小写可读字符串 ·[系统优化] 模块市场显示样式和文案 ·[系统优化] 支付中心回调日志优化完善 ·[系统优化] 系统集成时源代码编辑行距问题优化 ·[系统优化] 系统升级界面日志颜色优化 ·[系统优化] 路由请求定义优化 ·[系统优化] 用户VIP开通支付实现方式 ·[系统优化] 配置函数根据默认值类型自动推断 ·[Bug修复] 数据库队列并发死锁问题 ·[Bug修复] 部分脏数据导致的试卷答案页面报错 ·[Bug修复] 题目分类缓存清除异常问题 ·[Bug修复] 后台用户状态修改失败问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值