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题)的更多相关文章

  1. LeetCode 刷题记录(6-10题)

    6 Z 字形变换(题目链接) class Solution: def convert(self, s, numRows): """ :type s: str :type ...

  2. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  3. Leetcode刷题记录(python3)

    Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...

  4. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  5. leetcode刷题--两数之和(简单)

    一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...

  6. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  7. LeetCode 刷题指南(1):为什么要刷题

    虽然刷题一直饱受诟病,不过不可否认刷题确实能锻炼我们的编程能力,相信每个认真刷题的人都会有体会.现在提供在线编程评测的平台有很多,比较有名的有 hihocoder,LintCode,以及这里我们关注的 ...

  8. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  9. Leetcode | 刷题日记(1)

    本文记录个人刷题记录 推荐两个刷题网站: 地址:https://leetcode.com/ 另外一个地址:http://www.lintcode.com/ 1.Write a SQL query to ...

随机推荐

  1. 面向对象 part7 class

    类的定义 类实际上是个“特殊的函数“,就像能够定义函数表达式和函数声明一样,类语法 有两个组成部分:类表达式和类声明式 类声明 类声明没有提升 静态方法 只有构造函数名可以调用,实例无法使用.常用于应 ...

  2. JavaScript学习笔记 - 进阶篇(6)- JavaScript内置对象

    什么是对象 JavaScript 中的所有事物都是对象,如:字符串.数值.数组.函数等,每个对象带有属性和方法. 对象的属性:反映该对象某些特定的性质的,如:字符串的长度.图像的长宽等: 对象的方法: ...

  3. 架构之道(5) - APP和Web的后台架构

    当一个项目,同时需要Web.手机H5.Android,三平台同时可以测览,那就需要很简洁而有力的架构. 而我这就经历了这麽一个项目,先开发网站,然后是手机H5,最后是Android. 自信男人,无须多 ...

  4. 使用tensorflow的retrain.py训练图片分类器

    参考 https://hackernoon.com/creating-insanely-fast-image-classifiers-with-mobilenet-in-tensorflow-f030 ...

  5. 安装R的h5包

    系统 redhat7 安装H5的包, 依赖系统hdf5包,如下安装完, 发现版本不一致, sudo yum install hdf5-devel 解决办法: 移花接木 sudo ln -s /opt/ ...

  6. python 3.6

    安装了最新版anaconda3-4.3 发现jupyter-notebook 少了一些东西.需要手工安装 https://github.com/Anaconda-Platform/nbpresent

  7. logstash 使用glusterfs网络存储偶发性文件解析异常的问题

    其实问题到现在为止也没有解决 因为服务是部署在k8s上,挂载的,偶发性的出现文件解析异常 bom头已经验证过了 手动重新解析这些文件完全正常,问题无法复现,文件本身并没有问题. 最后怀疑到了最不该怀疑 ...

  8. 使用xb文件恢复mysql数据

    1.安装工具Percona XtraBackup MySQL 5.6及之前的版本需要安装 Percona XtraBackup 2.3,安装指导请参见官方文档Percona XtraBackup 2. ...

  9. Opencv笔记(五)——把鼠标当画笔

    学习目标:  学习使用 OpenCV 处理鼠标事件 学会使用函数cv2.setMouseCallback() 简单演示:         首先我们来创建一个鼠标事件回调函数,但鼠标事件发生是他就会被执 ...

  10. vue 利用axios请求接口下载excel

    一般有三种方法: 方法一: 通过a标签下载 // href为文件的存储路径或者地址,download为问文件名 <a href="/images/download.jpg" ...