1、数组

三数之和

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/3sum

class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def double_pointer_search(left, right, first_num, nums, result):
while left < right:
if 0 < left < right and nums[left] == nums[left - 1] or first_num + nums[left] + nums[right] < 0:
left += 1
if left < right < len(nums)-1 and nums[right] == nums[right + 1] or first_num + nums[left] + nums[right] > 0:
right -= 1
else:
result.append([first_num, nums[left], nums[right]])
left += 1
right -= 1 result = []
if len(nums) < 3:
return result nums.sort()
n = len(nums)
for i in range(n - 2):
if nums[i] + nums[i + 1] + nums[i + 2] > 0:
break
if nums[i] + nums[-1] + nums[-2] < 0:
continue
if i > 0 and nums[i] == nums[i - 1]:
continue
double_pointer_search(i + 1, n - 1, nums[i], nums, result) return result

2、排序

最大数

给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。

示例 1:

输入: [10,2]
输出: 210
示例 2:

输入: [3,30,34,5,9]
输出: 9534330

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/largest-number

from functools import cmp_to_key
class Solution(object):
def largestNumber(self, nums):
"""
:type nums: List[int]
:rtype: str
"""
def new_sort(num1, num2):
if num1 + num2 > num2 + num1:
return -1
elif num1 + num2 < num2 + num1:
return 1
return 0 result = "".join(sorted(list(map(lambda num: str(num), nums)), key=cmp_to_key(lambda x, y: new_sort(x, y))))
while result.startswith("0") and len(result) > 1:
result = result[1:] return result

3、多维数组

区间列表的交集

给定两个由一些闭区间组成的列表,每个区间列表都是成对不相交的,并且已经排序。

返回这两个区间列表的交集。

(形式上,闭区间 [a, b](其中 a <= b)表示实数 x 的集合,而 a <= x <= b。两个闭区间的交集是一组实数,要么为空集,要么为闭区间。例如,[1, 3] 和 [2, 4] 的交集为 [2, 3]。)

示例:

输入:A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
输出:[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
注意:输入和所需的输出都是区间对象组成的列表,而不是数组或列表。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/interval-list-intersections

class Solution(object):
def intervalIntersection(self, A, B):
"""
:type A: List[List[int]]
:type B: List[List[int]]
:rtype: List[List[int]]
"""
result = []
i, j = 0, 0
while i < len(A) and j < len(B):
left, right = max(A[i][0], B[j][0]), min(A[i][1], B[j][1])
if left <= right:
result.append([left, right])
if A[i][1] < B[j][1]:
i += 1
else:
j += 1
return result

4、特殊矩阵

01 矩阵

给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。

两个相邻元素间的距离为 1 。

示例 1:
输入:

0 0 0
0 1 0
0 0 0
输出:

0 0 0
0 1 0
0 0 0
示例 2:
输入:

0 0 0
0 1 0
1 1 1
输出:

0 0 0
0 1 0
1 2 1

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/01-matrix

class Solution(object):
def updateMatrix(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: List[List[int]]
"""
def bfs(i, j):
que, distance, visited = [(i, j)], 0, set()
while que:
distance += 1
new_que = []
for new_point in que:
for i, j in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
new_i, new_j = new_point[0] + i, new_point[1] + j
if 0 <= new_i < len(matrix) and 0 <= new_j < len(matrix[0]) and (new_i,new_j) not in visited:
if matrix[new_i][new_j] != 0:
new_que.append((new_i, new_j))
visited.add((new_i, new_j))
else:
return distance
que = new_que
return distance new_matrix = [[0 for i in range(len(matrix[0]))] for j in range(len(matrix))]
for row_index, row in enumerate(matrix):
for col_index, point in enumerate(row):
if matrix[row_index][col_index] != 0:
new_matrix[row_index][col_index] = bfs(row_index, col_index)
return new_matrix

5、查找

在排序数组中查找元素的第一个和最后一个位置

class Solution(object):
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
if not nums:
return [-1, -1]
left, right = 0, len(nums) - 1
while left + 1 < right:
mid = left + (right - left) / 2
if nums[mid] == target:
i = mid - 1
while i >= 0 and nums[i] == target:
i -= 1
j = mid + 1
while j < len(nums) and nums[j] == target:
j += 1
return [i + 1, j - 1]
if nums[mid] > target:
right = mid
elif nums[mid] < target:
left = mid if nums[left] == target and nums[right] == target:
return [left, right]
if nums[right] == target:
return [right, right]
if nums[left] == target:
return [left, left] return [-1, -1]

6、字符串

单词拆分 II

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。

说明:

分隔时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
示例 1:

输入:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
输出:
[
  "cats and dog",
  "cat sand dog"
]
示例 2:

输入:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
输出:
[
  "pine apple pen apple",
  "pineapple pen apple",
  "pine applepen apple"
]
解释: 注意你可以重复使用字典中的单词。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-break-ii

class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: List[str]
"""
def dfs(s, word_dict, sets):
if s in sets:
return sets[s] if len(s) == 0:
return [] partitions = []
if s in word_dict:
partitions.append(s) for i in range(1, len(s)):
word = s[:i]
if word not in word_dict:
continue
sub_partitions = dfs(s[i:], word_dict, sets)
partitions.extend(map(lambda sub_partition: word + " " + sub_partition, sub_partitions))
# for sub_partition in sub_partitions:
# partitions.append(word + " " + sub_partition) sets[s] = partitions
return partitions return dfs(s, set(wordDict), {})

7、最长子串

最长回文子串

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。

说明:

分隔时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
示例 1:

输入:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
输出:
[
  "cats and dog",
  "cat sand dog"
]
示例 2:

输入:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
输出:
[
  "pine apple pen apple",
  "pineapple pen apple",
  "pine applepen apple"
]
解释: 注意你可以重复使用字典中的单词。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-break-ii

class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if not s:
return "" n, start, longest = len(s), 0, 1
f = [[False]*n for _ in range(n)]
for i in range(n-1, -1, -1):
f[i][i] = True
for j in range(i + 1, n):
f[i][j] = s[i] == s[j] and (j - i < 2 or f[i+1][j-1])
if f[i][j] and longest < j - i + 1:
longest = j - i + 1
start = i return s[start:start + longest]

8、链表

旋转链表

给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。

示例 1:

输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL
解释:
向右旋转 1 步: 5->1->2->3->4->NULL
向右旋转 2 步: 4->5->1->2->3->NULL
示例 2:

输入: 0->1->2->NULL, k = 4
输出: 2->0->1->NULL
解释:
向右旋转 1 步: 2->0->1->NULL
向右旋转 2 步: 1->2->0->NULL
向右旋转 3 步: 0->1->2->NULL
向右旋转 4 步: 2->0->1->NULL

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-list

class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if not head:
return head
cur_node = head
size = self.calculate_size(cur_node)
k = k % size
if k == 0:
return head
cur_node = head
new_head = self.get_new_head(cur_node, k, size)
cur_node = new_head
self.attach_two_linked_list(cur_node, head) return new_head def attach_two_linked_list(self, cur_node, head):
while cur_node.next:
cur_node = cur_node.next
cur_node.next = head def get_new_head(self, cur_node, k, size):
len = 1
while len < size - k:
len += 1
cur_node = cur_node.next
new_head = cur_node.next
cur_node.next = None
return new_head def calculate_size(self, node):
size = 0
while node != None:
size += 1
node = node.next
return size

leetcode刷题(一)的更多相关文章

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

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

  2. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

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

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

  4. leetcode刷题记录--js

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

  5. LeetCode刷题总结之双指针法

    Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...

  6. Leetcode刷题记录(python3)

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

  7. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

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

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

  9. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

  10. LeetCode刷题总结-树篇(下)

    本文讲解有关树的习题中子树问题和新概念定义问题,也是有关树习题的最后一篇总结.前两篇请参考: LeetCode刷题总结-树篇(上) LeetCode刷题总结-树篇(中) 本文共收录9道题,7道中等题, ...

随机推荐

  1. java后端整合极光消息推送

    目录 1.简介 2.极光Demo 2.1.进入极光官网--应用管理 2.2.快速集成一个Android/iOS的SDK​ 2.3.java服务端代码 3.参考资料 1.简介 简单来说,就是androi ...

  2. [编程基础] Python中*args和**kwargs参数的使用

    本文主要介绍Python中*args和**kwargs参数的使用 文章目录 1 使用 2 拓展 3 参考 1 使用 在Python中,定义函数时可以使用两个特殊符号,以允许它们接受可变数量的参数.这两 ...

  3. [编程基础] Python日志记录库logging总结

    Python日志记录教程展示了如何使用日志记录模块在Python中进行日志记录. 文章目录 1 介绍 1.1 背景 1.2 Python日志记录模块 1.3 根记录器 2 Python logging ...

  4. python进阶之路20 正则表达式 re模块

    正则表达式前情 案例:京东注册手机号校验 基本需求:手机号必须是11位.手机号必须以13.15.17.18.19开头.必须是纯数字 '''纯python代码实现''' # while True: # ...

  5. 宇宙无敌搞笑轻松弄懂java动态代理

    https://www.cnblogs.com/ferryman/p/13170057.html jdk动态代理和cglib动态代理区别 https://blog.csdn.net/shallynev ...

  6. 异常概念&异常体系-异常分类

    异常概念&异常体系 异常,就是不正常的意思.在生活中:医生说,你的身体某个部分有异常,该部位和正常相比有点不同,该部位的功能将受影响,在程序中的意思就是: 异常:指的是程序在执行过程中,出现的 ...

  7. .NET与大数据

    前言 当别人做大数据用Java.Python的时候,我使用.NET做大数据.数据挖掘,这确实是值得一说的事. 写的并不全面,但都是实际工作中的内容. .NET在大数据项目中,可以做什么? 写脚本(使用 ...

  8. Python装饰器实例讲解(一)

    Python装饰器实例讲解(一) 多种角度讲述这个知识,这是个系列文章 但前后未必有一定的顺承关系 部分参考网络 本文以一个小案例引出装饰器的一些特点,不涉及理论,后面再谈 案例 写一个代码来求一个数 ...

  9. 快速入门API Explorer

    摘要:华为云API Explorer为开发者提供一站式API解决方案统一平台,集成华为云服务所有开放 API,支持全量快速检索.可视化调试.帮助文档.代码示例等能力,帮助开发者快速查找.学习API和使 ...

  10. Java堆外缓存(一个很有意思的应用)

    我们在开发过程中会遇到这样的场景:就是一个服务的各项 JVM 的配置都比较合理的情况下,它的 GC 情况还是不容乐观.分析之后发现有 2 个对象特别巨大,占了总存活堆内存的 90%以上.其中第 1 大 ...