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 ...
随机推荐
- Java基础二(2020.1.14)
学习内容: 1.Java运算符:赋值运算符,算术运算符,关系运算符,逻辑运算符,条件运算符 2.java流程控制:顺序,选择 1.java输入 Scanner s = new Scanner(Syst ...
- Spire.doc jar包实现word文件添加水印demo
/** * @create: 2020-02-23 21:50 */ import com.spire.doc.*; import com.spire.doc.documents.WatermarkL ...
- 基于Dijsktra算法的最短路径求解
基于Dijsktra算法的最短路径求解 描述 一张地图包括n个城市,假设城市间有m条路径(有向图),每条路径的长度已知.给定地图的一个起点城市和终点城市,利用Dijsktra算法求出起点到终点之间 ...
- unless|until|LABEL|{}|last|next|redo| || |//|i++|++i
#!/usr/bin/perl use strict; use warnings; $_ = 'oireqo````'; unless($_ =~ /^a/m){print "no matc ...
- B - Sequence II (HDU 5147)
Long long ago, there is a sequence A with length n. All numbers in this sequence is no smaller than ...
- PHP验证电子邮件-密码保护和随机密码
验证邮箱: function isValidEmail($email){ return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a ...
- QLIKVIEW添加数据库连接
1.点击文件 2.点击编辑脚本 3.点击 工具-ODBC管理员 4.添加DSN 5.里面的常规操作不再赘述 6.完成
- 73)PHP,session基本操作
(1)先开启: Session_start(); 也可以通过php.ini配置文件中,自动开启sesssion机制: Session.auto_start; (2)利用$_SESSION来操作数 ...
- HTML语言 网页制作-----标签、表格、表单、框架
一:序 Html静态网页,内容(hyper text markup language,超文本标记语言) Css 网页美化 Javascript 脚本语言 二:html的介绍 <!DOCTYPE ...
- WIN10 蓝牙连接音箱之后,音量调节无效,音量从1-100,声音一样大,都是最大声,可以静音(解决方案)
1.win+r,输入regedit,打开注册表2.进入路径:计算机\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Bluetooth\Audio\AV ...