You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step. Return the minimum number of steps to walk from the upper left corner (0, 0) …
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest-path-visiting-all-nodes/description/ 题目描述: An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.length = N,…
题目如下: Consider a directed graph, with nodes labelled 0, 1, ..., n-1.  In this graph, each edge is either red or blue, and there could be self-edges or parallel edges. Each [i, j] in red_edges denotes a red directed edge from node i to node j.  Simila…
题目如下: In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that: Adjacent cells C_i and C_{i+1} are connected 8-…
题目 非常简单的BFS 暴搜 struct Node { int x; int y; int k; int ans; Node(){} Node(int x,int y,int k,int ans) { this->x=x; this->y=y; this->k=k; this->ans=ans; } }; class Solution { public: int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; int vis[105][105]; i…
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/simplify-path/description/ 题目描述: Given an absolute path for a file (Unix-style), simplify it. For e…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/minimum-path-sum/description/ 题目描述 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which mi…
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 解题思路: 典型的动态规划.开辟…
题目链接 Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can move up, down, left or right from and to an empty cell. Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 题目描述 Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀是否回文 判断前缀 相似题目 参考资料 日期 题目地址:https://leetcode.com/problems/shortest-palindrome/description/ 题目描述 Given a string s, you are allowed to convert it to a palindrome by adding char…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetcode.com/problems/shortest-unsorted-continuous-subarray/description/ 题目描述 Given an integer array, you need to find one continuous subarray that if you o…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leetcode.com/problems/shortest-distance-to-a-character/description/ 题目描述 Given a string S and a character C, return an array of integers representing the…
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For example: Given "aacecaaa", retur…
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider th…
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 题解: Solution 1 () class Solut…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+暴力检索 日期 题目地址:https://leetcode-cn.com/problems/shortest-word-distance-iii/ 题目描述 Given a list of words and two words word1 and word2, return the shortest distance between these…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+二分查找 日期 题目地址:https://leetcode-cn.com/problems/shortest-distance-to-target-color/ 题目描述 You are given an array colors, in which there are three colors: 1, 2 and 3. You are also…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://leetcode-cn.com/problems/shortest-word-distance-ii/ 题目描述 Design a class which receives a list of words in the constructor, and implements a method that…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode-cn.com/problems/shortest-word-distance/ 题目描述 Given a list of words and two words word1 and word2, return the shortest distance between these two word…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + BFS 相似题目 参考资料 日期 题目地址:https://leetcode.com/problems/shortest-bridge/description/ 题目描述 In a given 2D binary array A, there are two islands. (An island is a 4-directionally…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/shortest-completing-word/description/ 题目描述 Find the minimum length word from a given dictionary words, which has all the letters from the…
算法导论上一道dp,挺有趣的.于是就研究了一阵. dp(i, j)代表从左边第一个点到第i个点与从从左边最后一个点(即为第一个点)到j点的最优距离和.于是找到了子状态. 决策过程 dp[i][j] = min{dp[i-1][j] + Dis(i, i - 1), dp[i - 1][k] + Dis(k, i)} 即可.代表意思是选择最优的路径加入到回路中的去途或者归途中. 一下是代码. /****************************************/ /***** Des…
题目如下: 解题思路:本题我采用的是最简单最直接最粗暴的方法,把排序后的nums数组和原始数组比较即可得到答案. 代码如下: /** * @param {number[]} nums * @return {number} */ var findUnsortedSubarray = function(nums) { var ol = nums.slice(0).sort(function(v1,v2){ return v1 - v2 }) var start = undefined var end…
题目如下: In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the small…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/maximize-distance-to-closest-person/description/ 题目描述 In a row of seats, 1 represents a person sitting in that seat, and 0 represents that…
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/path-sum-ii/description/ 题目描述: Given a binary tree and a sum, find all root-to-leaf paths where each…
[LeetCode]Minimum Depth of Binary Tree 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. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: im…
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall算法 Bellman-Ford算法 最小生成树 Kruskal算法 Prim算法 拓扑排序 双指针 动态规划 状态搜索 贪心 本文的目的是收集一些典型的题目,记住其写法,理解其思想,即可做到一通百通.欢迎大家提出宝贵意见! 博主有算法题解的微信公众号啦,欢迎关注「负雪明烛」,持续更新算法题的解题思路…
[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 - n 的所有形态的BST. 题解:枚举每个根节点 r, 然后递归的生成左右子树的所有集合,然后做笛卡尔积. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * Tr…