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 ...
随机推荐
- AtCoder Beginner Contest 129
ABCD 签到(A.B.C过水已隐藏) #include<bits/stdc++.h> using namespace std; ; int n,m,ans,f1[N][N],f2[N][ ...
- Mac下Rblas配置
Mac 下加速 R做矩阵计算, 感觉比没有R open 快, 但数量级上差不多了. 安装R open时,提示 X11相关的组件缺失,不想再额外安装,怕污染系统文件. cd /Library/Frame ...
- 【转】nginx如何设置防盗链
转自博客园作者:howhy,文章地址:nginx如何设置防盗链.大佬写的甚好,在此备份一下 关于nginx防盗链的方法网上有很多教程,都可以用,但是我发现很多教程并不完整,所做的防盗链并不是真正的彻底 ...
- Part-接口测试2
1.JsonPath:像xpath一样,提取json数值 2.json schema:github -> jsonschema from jsonshema import validate sc ...
- redis的集群:
集群策略:主从复制哨兵集群 参考:https://blog.csdn.net/q649381130/article/details/79931791 集群又分为如下:客户端分片基于代理的分片路由查询参 ...
- kettle的基本使用
一.下载下载kettlehttp://sourceforge.net/projects/pentaho/files/Data%20Integration/7.0/pdi-ce-7.0.0.0-25.z ...
- surprise库使用
自动交叉使用法 #-*- coding:utf-8 -*- from surprise import SVD from surprise import Dataset from surprise.mo ...
- mysql中in和exist的区别
mysql中in和exists的区别 -- in写法select * from A where A.id in (select bid from B ) and A.name in (select ...
- python 入门手册
python 入门手册 Introduction 这个手册是为了让学习者好好的重塑对于编程的认识,我们要来认识到怎么来在陌生的领域学习,学习的技巧开始,通过官方文档来学习 开端 利用IDE输出&quo ...
- django框架基础-视图系统-长期维护
################## 什么是视图? ####################### 视图: 1,一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受W ...