
leetcode题解
将leetcode中的题目按照算法思想进行分类,整理成,1、双指针、2、排序、3、贪心算法、4、二分查找、5、分治、6、搜索、7、动态规划、8、数学问题,9、数据结构相关问题一共9个分类进行分类整理。
-出发-
学生一个,正处于学习阶段,希望与大家多多交流
QQ:1515916596
-
原创 Leetcode题解-数据结构-树(BST)(python版)
文章目录1、修剪二叉查找树2、二叉查找树的第 k 个元素3、把二叉查找树每个节点的值都加上比它大的节点的值4、二叉查找树的最近公共祖先5、二叉树的最近公共祖先6、 有序数组构造二叉查找树7、 有序链表构造二叉查找树8、寻找两个点,和为给定值9、二叉搜索树中两节点差的最小值10、寻找二叉查找树中出现次数最多的值1、修剪二叉查找树669. 修剪二叉搜索树(Medium)# Definition for a binary tree node.# class TreeNode:# def __in2021-03-07 22:19:1151
0
-
原创 Leetcode题解-数据结构-树(python版)
文章目录1、递归1.1 树的高度1.2 平衡树1.3 归并两棵树1.4 判断是否存在一条路径和等于一个数1.5 统计路径和等于某个数的路径总数1.6 子树1.7 判断树是否对称1.8 两节点间的最长路径1.9 翻转树1.10 最小路径1.11 统计左叶子结点的和1.12 相同节点的最大路径长度1.13 间隔层序遍历1.14 二叉树中第二小的结点2、层序遍历2.1 二叉树每层节点的平均值2.2 找树左下角的结点2.3 之字形打印二叉树3、前中后序遍历3.1 非递归实现二叉树前序遍历3.2 非递归实现二叉树中序2021-03-02 00:40:0570
0
-
原创 Leetcode题解-数据结构-字符串(python版)
1、两字符的组成字符是否相等242. 有效的字母异位词(Easy)方法一:排序class Solution(object): def isAnagram(self, s, t): return sorted(s) == sorted(t)方法二:哈希表统计 s 和 t 中每一个字符出现的次数class Solution(object): def isAnagram(self, s, t): return collections.Counter(s2020-09-09 00:10:27150
0
-
原创 Leetcode题解-算法-搜索(python版)
文章目录1、BFS1.1 将一个数分解为整数的平方和1.2 最短单词路径1.3 K 站中转内最便宜的航班2、DFS2.1 查找最大的连通面积2.2 矩阵中的连通分量数目2.3 朋友圈的数量2.4 填充封闭的区域2.5 能到达的太平洋和大西洋的区域1、BFS1.1 将一个数分解为整数的平方和1.2 最短单词路径1.3 K 站中转内最便宜的航班bfs,广度优先搜索,先处理数据,以出发点为 key,到达站和价格为 value 构建字典,对处理好的数据进行 bfs。队列中保存的数据是站的位置,中转次数,从2020-08-18 00:27:29162
0
-
原创 Leetcode题解-算法-数组与矩阵
文章目录1、把数组中的0移动到数组尾部2、改变矩阵分维度3、数组中连续1的最大个数4、有序矩阵中查找目标数1、把数组中的0移动到数组尾部283. Move Zeroes(Easy)Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order...2019-08-11 21:19:39133
1
-
原创 Leetcode题解-数据结构-树(BST)
文章目录1、修剪二叉查找树2、1、修剪二叉查找树669. Trim a Binary Search Tree(Easy)给定一个二叉树,和一个范围 [L, R] (R >= L),只保留结点值在该范围中的结点。Example 1:Input: 1 / \ 0 2 L = 1 R = 2Output: 1 ...2019-08-09 16:30:4465
0
-
原创 Leetcode题解-算法-链表
文章目录1、找出两个链表的交点1、找出两个链表的交点160. Intersection of Two Linked Lists(Easy)给两个链表,找出链表的交点,没有交点输出NULL。设置指针 l1 指向链表 A 头节点,当访问到链表 A 的尾节点时,从链表 B 头部开始访问。设置指针 l2 指向链表 B 头节点,当访问到链表 B 的尾节点时,从链表 A 头部开始访问。设链表 A...2019-08-02 23:29:07239
0
-
原创 Leetcode题解-算法-搜索
文章目录1、BFS1.1 将一个数分解为整数的平方和1、BFS1.1 将一个数分解为整数的平方和279. Perfect Squares(Medium)Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to...2019-08-28 16:02:51112
0
-
原创 Leetcode题解-算法-贪心算法
最长的链646. Maximum Length of Pair Chain(Medium)You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.Now, we define a pair (c, d) can follow anoth...2019-07-27 12:24:00301
0
-
原创 Leetcode题解-算法-动态规划
文章目录1、斐波那契数列1.1爬楼梯1、斐波那契数列1.1爬楼梯70. Climbing Stairs(Easy)You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways...2019-06-02 20:02:43267
0
-
原创 Leetcode题解-算法-栈和队列
文章目录1、用栈实现队列1、用栈实现队列232. Implement Queue using Stacks(Easy)Implement the following operations of a queue using stacks.push(x) – Push element x to the back of queue.pop() – Removes the element fr...2019-05-23 22:25:0389
0
-
原创 Leetcode题解-算法-数学
文章目录1、最长上升子序列每一个数都可以分解成素数的乘积。令 x = 2m0 * 3m1 * 5m2 * 7m3 * 11m4 * …令 y = 2n0 * 3n1 * 5n2 * 7n3 * 11n4 * …如果 x 整除 y(y mod x == 0),则对于所有 i,mi <= ni。最大公约数最小公倍数x 和 y 的最大公约数为:gcd(x,y) = 2min(m0,n0...2019-12-03 09:27:23215
0
-
原创 Leetcode题解-算法-二分法
文章目录1、求开方2、找大于给定字符的最小字符1、求开方69. Sqrt(x)(Easy)Implement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer.Since the return type is an integ...2019-04-25 20:22:34178
0
-
原创 Leetcode题解-数据结构-字符串
Leetcode :242. Valid Anagram(Easy)问题描述Given two strings s and t , write a function to determine if t is an anagram of s.Example 1:Input: s = “anagram”, t = “nagaram”Output: trueExample 2:Input:...2019-03-31 23:41:33130
0
-
原创 Leetcode题解(更新中……)
算法思想双指针Two Sum II - Input array is sorted排序快速选择堆排序桶排序荷兰国旗问题贪心算法二分查找分治搜索BFSDFSBacktracking动态规划斐波那契数列矩阵路径数组区间分割整数最长递增子序列最长公共子序列0-1 背包数学素数最大公约数进制转换阶乘字符串加法减法相遇问题多数投票问题其它数据结...2019-03-13 21:37:28602
1
-
原创 Leetcode题解-数据结构-树
104. Maximum Depth of Binary Tree (Easy)题目描述Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf ...2019-03-08 20:19:07266
0
-
原创 Leetcode题解-数据结构-双指针
Leetcode :167. Two Sum II - Input array is sorted (Easy)采用双指针遍历数组,协调两个指针指向不同的位置来完成任务。题目描述Given an array of integers that is already sorted in ascending order, find two numbers such that they add up...2019-03-05 20:41:47126
0
-
原创 Leetcode题解-算法-哈希表
文章目录1、两数之和2、判断数组中是否有重复的数3、最长和谐序列1、两数之和1. Two Sum(Easy)给一个无须数组,找出两个下标,下标对应的数之和等于给定值,两下标不能相同。Example:Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1]....2019-08-28 15:57:4974
0
-
原创 Leetcode题解-算法-位运算
文章目录1 统计两个数二进制位多少位不同2 数组中唯一一个不重复的数3 寻找数组中缺失的数41 统计两个数二进制位多少位不同461. Hamming Distance(Easy)The Hamming distance between two integers is the number of positions at which the corresponding bits are dif...2019-09-16 12:00:4962
0