LeetCode 刷题记录(1-5题)
1 两数之和(题目链接)
class Solution: # 一次哈希法
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
m = {}
for i in range(len(nums)):
minus = target - nums[i]
if minus in m and i != m[minus]:
return [i, m[minus]]
m[nums[i]] = i
return None
2 两数相加(题目链接)
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
up = 0
l3 = ListNode(0)
l3_head = l3
while l1 or l2:
if l1 is None:
l1 = ListNode(0)
if l2 is None:
l2 = ListNode(0)
sum = l1.val + l2.val + up
if sum > 9:
sum = sum - 10
l3.next = ListNode(sum)
up = 1
else:
l3.next = ListNode(sum)
up = 0
l1 = l1.next
l2 = l2.next
l3 = l3.next
if up > 0:
l3.next = ListNode(up)
return l3_head.next
3 无重复字符的最长子串(题目链接)
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
num = 0
temp = []
for item_s in s:
if item_s in temp:
index = temp.index(item_s) + 1
temp = temp[index:]
temp.append(item_s)
else:
temp.append(item_s)
if len(temp) > num:
num = len(temp)
return num
4 寻找两个有序数组的中位数(题目链接)
class Solution:
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
sum = (len(nums1) + len(nums2))
index = sum//2 + 1
p1 = 0
p2 = 0
max1 = 0
max2 = 0 for item in range(index):
max2 = max1 if p1 == len(nums1):
max1 = nums2[p2]
p2 += 1
elif p2 == len(nums2):
max1 = nums1[p1]
p1 += 1
elif nums1[p1] > nums2[p2]:
max1 = nums2[p2]
p2 += 1
else:
max1 = nums1[p1]
p1 += 1 if sum%2 == 1:
return max1
else:
return (max1 + max2)/2
5 最长回文子串(题目链接)
Manacher算法(“马拉车算法”,中心扩展法+不重复判断)
讲解链接:https://www.jianshu.com/p/116aa58b7d81
https://www.cnblogs.com/nkqlhqc/p/9005450.html
class Solution:
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if s == '':
return '' sp = ''
sp_len = 2*len(s)+2
radius = [0 for o in range(sp_len)] # 回文半径列表
index_max = -1 # 最大回文子串中间所在index
r_max = -1 # 最大回文子串右边界 def plalindrome(index, r_i=1):
while(sp[index-r_i] != '$' and sp[index+r_i] != '$' and sp[index-r_i] == sp[index+r_i]):
r_i += 1
return r_i for item in range(len(s)): # 字符串的字符间加'#'与'$'
sp += ''.join(['#', s[item]])
sp = ''.join(['$', sp, '#$']) for i in range(sp_len): # 计算回文半径,填充半径列表
if i >= r_max:
radius[i] = plalindrome(i)
if r_max < radius[i]:
r_max = radius[i]
index_max = i
elif r_max - i <= radius[2 * index_max - i]:
radius[i] = radius[2 * index_max - i]
else:
radius[i] = plalindrome(i, radius[2 * index_max - i] + 1)
if r_max < radius[i]:
r_max = radius[i]
index_max = i
result = sp[index_max - radius[index_max] + 1: index_max + radius[index_max]]
return result.replace('#', '')
LeetCode 刷题记录(1-5题)的更多相关文章
- LeetCode 刷题记录(6-10题)
6 Z 字形变换(题目链接) class Solution: def convert(self, s, numRows): """ :type s: str :type ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode刷题--两数之和(简单)
一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- LeetCode 刷题指南(1):为什么要刷题
虽然刷题一直饱受诟病,不过不可否认刷题确实能锻炼我们的编程能力,相信每个认真刷题的人都会有体会.现在提供在线编程评测的平台有很多,比较有名的有 hihocoder,LintCode,以及这里我们关注的 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- Leetcode | 刷题日记(1)
本文记录个人刷题记录 推荐两个刷题网站: 地址:https://leetcode.com/ 另外一个地址:http://www.lintcode.com/ 1.Write a SQL query to ...
随机推荐
- 基于Linux下的C语言项目实战--本地账号管理系统
C语言开发项目实战: C语言是一门通用计算机编程语言,广泛应用于底层开发.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言.尽 ...
- 吴裕雄--天生自然 PYTHON3开发学习:SMTP发送邮件
import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] ) SMTP.sendmail(from_addr, ...
- maven tomcat 自动部署配置
1:Tomacat 配置 /tomcat-users.xml 添加如下: <role rolename="manager-gui"/> <role rolenam ...
- 关于前端CSS的总结
CSS语法 CSS语言的基本单位是样式声明:propertyName : value ; CSS语言的使用方式: 1.把CSS样式声明作为HTML标签的style属性值.2.使用CSS选择器 CSS常 ...
- (转)Navicat Premium 12.1.8.0安装与激活
http://www.mamicode.com/info-detail-2493067.html
- ununtu 16.04 下的 VsCode 下载与安装
Vscode发现用包下载显示找不到网页,于是只有继续折腾. 折腾如下: ubuntu-desktop You can update your system with unsupported packa ...
- dedecms_5.7 download.php SQL注入
最近在看Web渗透与漏洞挖掘,这本书的编写目的感觉非常的不错,把渗透和代码审计结合在一起,但是代码审计部分感觉思路个人认为并不是很清晰,在学习dedecms v5.7 SQL注入的时候就只看懂了漏洞, ...
- 解决2013Lost connection to MySQL server during query错误方法
在my.ini配置文件 mysqld 节点下添加 max_allowed_packet = 500M 也就是配置MySQL允许的最大数据包大小,上面的500M你可以根据你的项目修改为你自己的值,只要比 ...
- 5-7 学生cpp成绩统计
完成“学生cpp成绩计算”之后,修改Person和Student类,各自增加两个无参构造函数. 仍以Person类为基础,建立一个派生类Teacher,增加以下成员数据: int ID;//教师工号 ...
- 阿里巴巴IconFont的使用方式
一.解释一下为什么要使用IconFont? IconFont顾名思义就是把图标用字体的方式呈现. 其优点在于以下几个方面: 1.可以通过css的样式改变其颜色:(最霸气的理由) 2.相对于图片来说,具 ...