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 ...
随机推荐
- drf序列化单改-整改-局部改-群改接口的实现
整体单改 路由层.模型层.序列化层不需要做修改,只需要处理视图层:views.py """ 1) 单整体改,说明前台要提供修改的数据,那么数据就需要校验,校验的数据应该在 ...
- 关于redis的使用总结
简介 redis是一个开源的用c语言编写的数据库,但并不像常规的关系型数据库那样把数据存储在表中 , 而是像Java中的Map集合一样,存储的是一系列key=value键值对,redis属于NoSQL ...
- 三、Shell脚本高级编程实战第三部
保养好自己的发动机:身体的和心里的身体健康:打打球,跑跑步心里健康:多和大家接触,保持家人.朋友的良好关系一.$*和$@的区别 $*:获取shell的所有参数,将所有的参数视为单个字符串. $ ...
- Android开发学习1----AndroidStudio的安装、创建第一个Android Studio文件、Android Studio界面介绍和HelloWord!
移动开发的工具有很多:Android Studio,eclipse,Hbuilder等,其中,现如今最火的开发工具是Android Studio,Android Studio是谷歌自己推出的一款集成开 ...
- linux下U盘变成只读文件系统的修复
问题描述: U盘插入linux下后变成了只读文件系统,不能再往u盘里拷贝文件. 总结:产生这个问题的原因可能是u盘文件系统损坏,操作系统为了防止它损坏系统,将它设置成了只读 修复方法: 在window ...
- Exynos4412开发板-网络-同一网段
1.1 同一网段在不少实验中,都会需要用到局域网的一些基础知识,在技术支持的过程中,发现不少用户对于这个概念非常模糊,导致 IP 地址或者网络环境稍微有点变化,就无法实现实验.如果没有接触过这个概念, ...
- eclipse web 项目配置 运行
- DataGrip设置时区
新版本DataGrip以默认时区取世界标准时间.要想时间显示正常,需要将时区变为上海时区,可手动在连接配置里设置参数.如下图: 操作步骤1.右键打开你想要修改的数据库连接的Properties菜单:2 ...
- 在服务器搭建git服务器
服务端操作 安装Git及创建用户组.用户 123 yum install gitgroupadd gitadduser git -g git 禁止用户git登录 修改/etc/passwd文件: 找到 ...
- JSON — Java与JSON数据互转
转换时Bean所要求的: 被转换的Bean必需是public的. Bean被转换的属性一定要有对应的get方法,且一定要是public的. Bean中不能用引用自身的this的属性,否则运行时出现et ...