
LeetCode
文章平均质量分 50
每日刷题笔记。
DrCrypto
这个作者很懒,什么都没留下…
展开
-
Leetcode 30.串联所有单词的子串
Time: 20191023题目描述给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。示例 1:输入: s = "barfoothefoobarman", words = ["foo","bar"]...原创 2019-10-23 14:38:17 · 1734 阅读 · 1 评论 -
Leetcode 1222.可以攻击国王的皇后
Time: 20191014题目描述在一个 8x8 的棋盘上,放置着若干「黑皇后」和一个「白国王」。「黑皇后」在棋盘上的位置分布用整数坐标数组 queens 表示,「白国王」的坐标用数组 king 表示。「黑皇后」的行棋规定是:横、直、斜都可以走,步数不受限制,但是,不能越子行棋。请你返回可以直接攻击到「白国王」的所有「黑皇后」的坐标(任意顺序)。示例 1:输入:queens = ...原创 2019-10-14 19:55:50 · 1584 阅读 · 0 评论 -
Leetcode 58 之反向迭代器的使用
题目: Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string. If the last word does not exist, return 0. For examp原创 2016-09-14 01:55:05 · 604 阅读 · 0 评论 -
LeetCode 35题解
35. Search Insert Position Difficulty: MediumGiven a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.原创 2016-09-07 22:17:28 · 799 阅读 · 0 评论 -
Leetcode 318答案详解(基于C++位操作)
随机Pick到Leetcode的318题,选择了C++基于STL的答题模式。 先看题目,Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that原创 2016-09-06 23:16:59 · 33585 阅读 · 1 评论 -
Leetcode 1. Two Sum
Leetcode 1. Two Sum@(LeetCode题目分析) 基于Python3题目描述Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exa原创 2017-11-04 09:15:26 · 496 阅读 · 0 评论 -
Leetcode 111
问题类型:Easy, BFS问题描述:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Note: A leaf is a n...原创 2018-10-09 12:09:43 · 385 阅读 · 0 评论 -
Leetcode 690
问题描述:简单型,BFS解法You are given a data structure of employee information, which includes the employee’s unique id, his importance value and his directsubordinates’ id.For example, employee 1 is the lead...原创 2018-10-09 15:35:11 · 358 阅读 · 0 评论 -
Leetcode 199
Type: Medium, BFS问题描述:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.Example:Input: [1,2,3,null,5,nul...原创 2018-10-10 10:33:40 · 332 阅读 · 0 评论 -
Leetcode 279 完美平方数
Type: Medium, BFS完美平方数题目描述:Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.Example 1:Input: n = 12Output: 3 Explanatio...原创 2018-10-12 11:21:16 · 1115 阅读 · 1 评论 -
【Leetcode 687】递归求最大相同路径
类型:Easy, 递归题目描述:Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.Note: The length of path betw...原创 2018-10-16 11:30:00 · 479 阅读 · 1 评论 -
Leetcode106 由中序序列和后序序列构建二叉树
最难的地方在于对序列的下标计算,比如给定一个序列,开始下标是i,那么下标i+1就是开始元素的后一个元素,注意到,1还是当前元素组成的列表的元素个数。即i + size,是包含i元素在内的size个数组的最后一个元素的后面一个位置的下标边界。所以对中序序列的划分,左子树是[inL, inL + leftTreeSize),右子树是[inL + leftTreeSize + 1, inR),其中in...原创 2019-01-21 16:10:12 · 500 阅读 · 0 评论 -
【Leetcode 795】Number of Subarrays with Bounded Maximum
难度:中等题目描述We are given an array A of positive integers, and two positive integers L and R (L <= R).Return the number of (contiguous, non-empty) subarrays such that the value of the maximum array ...原创 2019-02-21 20:38:53 · 366 阅读 · 0 评论 -
【LeetCode 148】链表的归并排序
上一篇文章用数组实现了二路归并排序,核心在于要对每次减半的元素进行实质性的改变,在数组中我们用临时数组记录每次merge的结果,在链表中用归并排序更加直接,因为我们操作的是结点的指针。题目描述难度:中等Sort a linked list in O(n log n) time using constant space complexity.Example 1:Input: 4->2...原创 2019-02-24 15:29:47 · 360 阅读 · 0 评论 -
LeetCode 144. 树的前序遍历迭代写法
Time: 20190224本题对应的是LeetCode 144,Medium难度(迭代算法的难度)。树的前序遍历Given a binary tree, return the preorder traversal of its nodes’ values.Example:Input: [1,null,2,3] 1 \ 2 / 3Output: ...原创 2019-02-24 20:56:09 · 702 阅读 · 0 评论 -
Leecode 953. Verifying an Alien Dictionary[Easy]
题目描述给定一个字符串数组和字典排序,这个字典序即外星人的字母顺序表,验证字符串数组是不是按照升序排列。题解写一个比较函数,然后两两验证。class Solution {public: // check if a is less than b bool check (string a, string b, unordered_map<char, int> &a...原创 2019-03-29 20:50:13 · 316 阅读 · 0 评论 -
Lintcode 51.上一个排列[Medium]
题目描述给定一个整数数组来表示排列,找出其上一个排列。排列是有序的,前一个比当前表示的数字小。题解class Solution {public: /* * @param nums: A list of integers * @return: A list of integers that's previous permuation */ vec...原创 2019-03-29 21:48:13 · 418 阅读 · 0 评论 -
Lintcode 138. 子数组之和 [Easy]
[20190331]题目描述给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置样例样例 1:输入: [-3, 1, 2, -3, 4]输出: [0,2] 或 [1,3]样例解释:返回任意一段和为0的区间即可。样例 2:输入: [-3, 1, -4, 2, -3, 4]输出: [1,5]注意事项:至少有一个子数组的和为 0题解可...原创 2019-03-31 14:03:08 · 560 阅读 · 2 评论 -
Leetcode 257. 二叉树的所有路径
Time: 2019-08-12题目描述给定一个二叉树,返回所有从根节点到叶子节点的路径。说明: 叶子节点是指没有子节点的节点。示例:输入: 1 / \2 3 \ 5输出: [“1->2->5”, “1->3”]解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3来源:力扣(LeetCode)...原创 2019-08-30 14:22:53 · 451 阅读 · 0 评论 -
103.二叉树的锯齿形层次遍历:DFS解法
Time: 20190830题目描述给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。例如:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回锯齿形层次遍历如下:[ [3], [20,9], [15,7]]...原创 2019-08-30 14:43:29 · 281 阅读 · 0 评论 -
Leetcode 5.最长回文子串
Time: 20190830题目描述给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。示例 1:输入: “babad”输出: “bab”注意: “aba” 也是一个有效答案。示例 2:输入: “cbbd”输出: “bb”来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/longest-p...原创 2019-08-30 19:19:44 · 194 阅读 · 0 评论 -
Leetcode120.三角形的最小路径和 -- DP算法
Time: 20190831题目描述给定一个三角形,找出自顶向下的最小路径和。每一步只能移动到下一行中相邻的结点上。例如,给定三角形:[ [2], [3,4], [6,5,7], [4,1,8,3]]自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。说明:如果你可以只使用 O(n)的额外空间(n 为三角形的总行数)来解决这个问题...原创 2019-08-31 18:26:03 · 451 阅读 · 0 评论 -
Leetcode 64. 最小路径和 -- DP算法
Time: 20190831题目描述给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。说明:每次只能向下或者向右移动一步。示例:输入:[ [1,3,1], [1,5,1], [4,2,1]]输出: 7解释: 因为路径 1→3→1→1→1 的总和最小。来源:力扣(LeetCode)链接:https://leet...原创 2019-08-31 18:43:52 · 312 阅读 · 0 评论 -
Leetcode 99. 恢复搜索二叉树
Time: 20190901题目描述二叉搜索树中的两个节点被错误地交换。请在不改变其结构的情况下,恢复这棵树。示例 1:输入: [1,3,null,null,2] 1 / 3 \ 2输出: [3,1,null,null,2] 3 / 1 \ 2示例 2:输入: [3,1,4,null,null,2] 3 / \1 4 ...原创 2019-09-01 10:30:00 · 382 阅读 · 0 评论 -
Leetcode 100.相同的树
Time: 20190901题目描述给定两个二叉树,编写一个函数来检验它们是否相同。如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。示例 1:输入: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3]输出: true示例 2:输入: 1 ...原创 2019-09-01 13:29:37 · 148 阅读 · 0 评论 -
Leetcode 101.对称二叉树
Time: 20190901题目描述给定一个二叉树,检查它是否是镜像对称的。例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 1 / \ 2 2 / \ / \3 4 4 3但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / \ 2 2 \ \ 3 3说明:如果你可以...原创 2019-09-01 13:34:59 · 184 阅读 · 0 评论 -
Leetcode 242. 有效的字母异位
Time: 20190901Type: Easy题目描述给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。示例 1:输入: s = “anagram”, t = “nagaram”输出: true示例 2:输入: s = “rat”, t = “car”输出: false说明:你可以假设字符串只包含小写字母。进阶:如果输入字符串包含 unic...原创 2019-09-01 13:43:10 · 249 阅读 · 0 评论 -
Leetcode 105. 前序和中序遍历序列构造二叉树
Time: 20190901Type: Medium题目描述根据一棵树的前序遍历与中序遍历构造二叉树。注意:你可以假设树中没有重复的元素。例如,给出前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7]返回如下的二叉树: 3 / \ 9 20 / \ 15 7来源:力扣(Lee...原创 2019-09-01 14:12:26 · 217 阅读 · 0 评论 -
Leetcode 106.从中序与后序遍历序列重建二叉树
Time: 20190901Type: Medium题目描述根据一棵树的中序遍历与后序遍历构造二叉树。注意:你可以假设树中没有重复的元素。例如,给出中序遍历 inorder = [9,3,15,20,7]后序遍历 postorder = [9,15,7,20,3]返回如下的二叉树: 3 / \ 9 20 / \ 15 7来源:力扣(Le...原创 2019-09-01 14:38:42 · 336 阅读 · 1 评论 -
Leetcode108. 有序数组转为二叉树
Time: 20190901Type: Easy题目描述将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。示例:给定有序数组: [-10,-3,0,5,9],一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树: 0 /...原创 2019-09-01 14:53:07 · 441 阅读 · 0 评论 -
Leetcode 109.有序链表转换二叉搜索树
Time: 20190901Type: Meidum题目描述给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。示例:给定的有序链表: [-10, -3, 0, 5, 9],一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜...原创 2019-09-01 15:59:51 · 146 阅读 · 0 评论 -
Leetcode 110.平衡二叉树
Time: 20190901Type: Easy题目描述思路DFS遍历时判断是否满足平衡树的性质,如果不满足返回-1,直接截断计算。代码# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left...原创 2019-09-01 16:12:38 · 217 阅读 · 0 评论 -
Leetcode 111.二叉树的最小深度
Time: 20190901Type: Easy题目描述给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回它的最小深度 2.来源:力扣(LeetCode...原创 2019-09-01 16:29:19 · 287 阅读 · 0 评论 -
Leetcode113.路径和II
Time: 20190901Type: Medium题目描述给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。说明: 叶子节点是指没有子节点的节点。示例:给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 / / \ ...原创 2019-09-01 16:48:48 · 240 阅读 · 0 评论 -
Leetcode 114.二叉树展开为链表
Time: 20190901Type: Medium题目描述给定一个二叉树,原地将它展开为链表。例如,给定二叉树 1 / \ 2 5 / \ \3 4 6将其展开为:1 \ 2 \ 3 \ 4 \ 5 \ 6来源:力扣(LeetCod...原创 2019-09-01 19:04:41 · 210 阅读 · 0 评论 -
Leetcode 116. 填充每个节点的下一个右侧节点指针
Time: 20190901Type: Medium题目描述给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:struct Node { int val; Node *left; Node *right; Node *next;}填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next...原创 2019-09-01 21:41:31 · 253 阅读 · 0 评论 -
Leetcode 124.二叉树中的最大路径和
Time: 20190901Type: Hard题目描述给定一个非空二叉树,返回其最大路径和。本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。示例 1:输入: [1,2,3] 1 / \ 2 3输出: 6示例 2:输入: [-10,9,20,null,null,15,7] ...原创 2019-09-01 22:28:29 · 239 阅读 · 0 评论 -
Leetcode 687.最长同值路径
Time: 20190901Type: Easy题目描述给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值。 这条路径可以经过也可以不经过根节点。注意:两个节点之间的路径长度由它们之间的边数表示。示例 1:输入: 5 / \ 4 5 / \ \ 1...原创 2019-09-01 22:51:26 · 349 阅读 · 0 评论 -
Leetcode 129. 根到叶子结点数字之和
Time: 20190902Type: Medium题目描述给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字。例如,从根到叶子节点路径 1->2->3 代表数字 123。计算从根到叶子节点生成的所有数字之和。说明: 叶子节点是指没有子节点的节点。示例 1:输入: [1,2,3] 1 / \ 2 3输...原创 2019-09-02 09:49:52 · 345 阅读 · 0 评论 -
Leetcode130.被围绕的区域
Time: 20190901Type: Medium题目描述给定一个二维的矩阵,包含 ‘X’ 和 ‘O’(字母 O)。找到所有被 ‘X’ 围绕的区域,并将这些区域里所有的 ‘O’ 用 ‘X’ 填充。示例:X X X XX O O XX X O XX O X X运行你的函数后,矩阵变为:X X X XX X X XX X X XX O X X解释:被围绕的区间不会存在...原创 2019-09-02 11:45:18 · 290 阅读 · 0 评论