算法
文章平均质量分 67
青铁
这个作者很懒,什么都没留下…
展开
-
*******地图分析
你现在手里有一份大小为N x N 的 网格 grid,上面的每个 单元格 都用0和1标记好了。其中0代表海洋,1代表陆地,请你找出一个海洋单元格,这个海洋单元格到离它最近的陆地单元格的距离是最大的。我们这里说的距离是「曼哈顿距离」(Manhattan Distance):(x0, y0) 和(x1, y1)这两个单元格之间的距离是|x0 - x1| + |y0 - y1|。如果网格上只有陆地或者海洋,请返回-1。示例 1:输入:[[1,0,1],[0,0,0...原创 2020-12-10 18:26:13 · 288 阅读 · 1 评论 -
****可能的二分法
给定一组N人(编号为1, 2, ..., N),我们想把每个人分进任意大小的两组。每个人都可能不喜欢其他人,那么他们不应该属于同一组。形式上,如果 dislikes[i] = [a, b],表示不允许将编号为 a 和 b 的人归入同一组。当可以用这种方法将所有人分进两组时,返回 true;否则返回 false。示例 1:输入:N = 4, dislikes = [[1,2],[1,3],[2,4]]输出:true解释:group1 [1,4], group2 [2,3]...原创 2020-12-06 17:12:44 · 235 阅读 · 1 评论 -
****重新安排行程
给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序。所有这些机票都属于一个从 JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 开始。提示:如果存在多种有效的行程,请你按字符自然排序返回最小的行程组合。例如,行程 ["JFK", "LGA"] 与 ["JFK", "LGB"] 相比就更小,排序更靠前所有的机场都用三个大写字母表示(机场代码)。假定所有机票至少存在一种合理的行程。所有的机票必须都用一次.原创 2020-12-03 14:48:13 · 213 阅读 · 0 评论 -
***判断二分图
给定一个无向图graph,当这个图为二分图时返回true。如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我们就将这个图称为二分图。graph将会以邻接表方式给出,graph[i]表示图中与节点i相连的所有节点。每个节点都是一个在0到graph.length-1之间的整数。这图中没有自环和平行边:graph[i]中不存在i,并且graph[i]中没有重复的值。示例 1:输入: [[1,3], [0,2], [1,3], ..原创 2020-11-30 15:18:39 · 89 阅读 · 0 评论 -
**概率最大的路径
给你一个由 n 个节点(下标从 0 开始)组成的无向加权图,该图由一个描述边的列表组成,其中 edges[i] = [a, b] 表示连接节点 a 和 b 的一条无向边,且该边遍历成功的概率为 succProb[i] 。指定两个节点分别作为起点 start 和终点 end ,请你找出从起点到终点成功概率最大的路径,并返回其成功概率。如果不存在从 start 到 end 的路径,请 返回 0 。只要答案与标准答案的误差不超过 1e-5 ,就会被视作正确答案。示例 1:输入:n = 3, .原创 2020-11-26 18:03:33 · 388 阅读 · 0 评论 -
**阈值距离内邻居最少的城市
有 n个城市,按从 0 到 n-1编号。给你一个边数组edges,其中 edges[i] = [fromi, toi, weighti]代表fromi和toi两个城市之间的双向加权边,距离阈值是一个整数distanceThreshold。返回能通过某些路径到达其他城市数目最少、且路径距离 最大 为distanceThreshold的城市。如果有多个这样的城市,则返回编号最大的城市。注意,连接城市 i 和 j 的路径的距离等于沿该路径的所有边的权重之和。示例 1:输...原创 2020-11-24 15:49:44 · 133 阅读 · 0 评论 -
**课程表 II
现在你总共有 n 门课需要选,记为0到n-1。在选修某些课程之前需要一些先修课程。例如,想要学习课程 0 ,你需要先完成课程1 ,我们用一个匹配来表示他们: [0,1]给定课程总量以及它们的先决条件,返回你为了学完所有课程所安排的学习顺序。可能会有多个正确的顺序,你只要返回一种就可以了。如果不可能完成所有课程,返回一个空数组。示例1:输入: 2, [[1,0]]输出: [0,1]解释:总共有 2 门课程。要学习课程 1,你需要先完成课程 0。因此,正确的课程顺序为 [...原创 2020-11-21 13:31:46 · 96 阅读 · 0 评论 -
**课程表 II
你这个学期必须选修 numCourse 门课程,记为0到numCourse-1 。在选修某些课程之前需要一些先修课程。例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们:[0,1]给定课程总量以及它们的先决条件,请你判断是否可能完成所有课程的学习?示例 1:输入: 2, [[1,0]]输出: true解释:总共有 2 门课程。学习课程 1 之前,你需要完成课程 0。所以这是可能的。示例 2:输入: 2, [[1,0],[0,1]]输出: ...原创 2020-11-20 18:16:50 · 127 阅读 · 0 评论 -
××将 x 减到 0 的最小操作数
给你一个整数数组nums和一个整数x。每一次操作时,你应当移除数组nums最左边或最右边的元素,然后从x中减去该元素的值。请注意,需要修改数组以供接下来的操作使用。如果可以将x恰好减到0,返回最小操作数;否则,返回-1。示例 1:输入:nums = [1,1,4,2,3], x = 5输出:2解释:最佳解决方案是移除后两个元素,将 x 减到 0 。BFS,超时class Solution { public int m...原创 2020-11-15 12:27:49 · 363 阅读 · 0 评论 -
**确定两个字符串是否接近
如果可以使用以下操作从一个字符串得到另一个字符串,则认为两个字符串接近:操作 1:交换任意两个现有字符。 例如,abcde-> aecdb 操作 2:将一个现有字符的每次出现转换为另一个现有字符,并对另一个字符执行相同的操作。 例如,aacabb->bbcbaa(所有a转化为b,而所有的b转换为a) 你可以根据需要对任意一个字符串多次使用这两种操作。给你两个字符串,word1和word2。如果word1和word2接近...原创 2020-11-15 12:15:27 · 113 阅读 · 0 评论 -
**跳跃游戏 II
给定一个非负整数数组,你最初位于数组的第一个位置。数组中的每个元素代表你在该位置可以跳跃的最大长度。你的目标是使用最少的跳跃次数到达数组的最后一个位置。示例:输入: [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2。 从下标为 0 跳到下标为 1 的位置,跳1步,然后跳3步到达数组的最后一个位置。说明:假设你总是可以到达数组的最后一个位置。class Solution { public int jump(int[] nums...原创 2020-11-13 18:06:40 · 94 阅读 · 0 评论 -
**监控二叉树
给定一个二叉树,我们在树的节点上安装摄像头。节点上的每个摄影头都可以监视其父对象、自身及其直接子对象。计算监控树的所有节点所需的最小摄像头数量。难点在于递归过程中,节点共有三种状态。class Solution { int res = 0; public int minCameraCover(TreeNode root) { if(root == null) return 0; if(helper(root) == 2) res+..原创 2020-11-11 16:46:35 · 82 阅读 · 0 评论 -
[LintCode]Insert Interval
[题目] : Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree.ExampleGiven binary search tree as follow原创 2016-02-16 00:08:47 · 302 阅读 · 0 评论 -
[LintCode]Insertion Sort List
Sort a linked list using insertion sort.ExampleGiven 1->3->2->0->null, return 0->1->2->3->null.1,使用dummy节点,便于处理插入位置为首位的问题。2,1个head指针,指向待处理list首位。3,1个临时变量,保存下一个head节点位置。此题一个指针+一个临时变原创 2016-02-16 18:33:55 · 266 阅读 · 0 评论 -
[LintCode]Invert Binary Tree
Invert a binary tree.DFS递归,依次交换左右节点public class Solution { /** * @param root: a TreeNode, the root of the binary tree * @return: nothing */ public void invertBinaryTree(Tree原创 2016-02-16 18:48:03 · 225 阅读 · 0 评论 -
[LintCode]Length of Last Word
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.遍历每个字符,每次原创 2016-02-17 04:56:40 · 255 阅读 · 0 评论 -
[Lintcode]Longest Increasing Continuous Subsequence
Give an integer array,find the longest increasing continuous subsequence in this array.An increasing continuous subsequence:Can be from right to left or from left to right.Indices of the原创 2016-02-17 05:27:03 · 278 阅读 · 0 评论 -
[Lintcode]Longest Words
Given a dictionary, find all of the longest words in the dictionary.(do it in one pass)简单题,直接写代码class Solution { /** * @param dictionary: an array of strings * @return: an arra原创 2016-02-17 05:37:28 · 350 阅读 · 0 评论 -
[Lintcode]Majority Number
Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it.要求:O(n) time and O(1) extra space投票算法, 一般需要再遍历一遍,为防止不存在majority num原创 2016-02-18 22:22:51 · 220 阅读 · 0 评论 -
[Lintcode] Matrix Zigzag Traversal
Given a matrix of m x nelements (m rows, ncolumns), return all elements of the matrix in ZigZag-order.ExampleGiven a matrix:[ [1, 2, 3, 4], [5, 6, 7, 8], [9,10, 11, 12]]r原创 2016-02-18 23:25:43 · 236 阅读 · 0 评论 -
[Lintcode]Maximum Depth of Binary Tree
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 node.ExampleGiven a binary tr原创 2016-02-19 17:52:01 · 330 阅读 · 0 评论 -
[Lintcode]Maximum Subarray
Given an array of integers, find a contiguous subarray which has the largest sumExampleGiven the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1]has the largest sum =原创 2016-02-19 18:38:19 · 357 阅读 · 0 评论 -
[Lintcode]Median
Given a unsorted array with integers, find the median of it. A median is the middle number of the array after it is sorted. If there are even numbers in the array, return the N/2-th number aft原创 2016-02-21 18:49:53 · 213 阅读 · 0 评论 -
[Lintcode]Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.ExampleA = [1, 2, 3, empty, empty], B = [4, 5]After merge, A will be filled as [1, 2, 3, 4, 5]如果从数组A的ind原创 2016-02-21 22:13:13 · 195 阅读 · 0 评论 -
*[Lintcode]Merge Sorted Array II
Merge two given sorted integer array A and B into a new sorted integer array.与上一道类似class Solution { /** * @param A and B: sorted integer array A and B. * @return: A new sor原创 2016-02-21 23:45:25 · 260 阅读 · 0 评论 -
[Lintcode]Merge Two Sorted Lists
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order.p原创 2016-02-22 00:22:58 · 247 阅读 · 0 评论 -
[Lintcode]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.ExampleGiven a binary tr原创 2016-02-22 23:34:31 · 320 阅读 · 0 评论 -
[Lintcode]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.可以用递归,二维DP或者一维DP解决。三种算法原理大体相同,在优化上一维DP使用滚动数组所原创 2016-02-22 23:58:31 · 319 阅读 · 0 评论 -
*[Lintcode]Minimum Subarray
Given an array of integers, find the subarray with smallest sum.Return the sum of the subarray.ExampleFor [1, -1, -2, 1], return -3题目和最大子串类似,解法也类似public class Sol原创 2016-02-23 05:01:45 · 339 阅读 · 0 评论 -
[Lintcode]Nth to Last Node in List
Find the nth to last element of a singly linked list. The minimum number of nodes in list is n.ExampleGiven a List 3->2->1->5->null and n = 2, return node whose value is 1.原创 2016-02-23 05:09:26 · 211 阅读 · 0 评论 -
[Lintcode]Number of Islands
Given a boolean 2D matrix, find the number of islands.ExampleGiven graph:[ [1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1]]return 3.原创 2016-02-23 05:21:58 · 300 阅读 · 0 评论 -
*[Lintcode]O(1) Check Power of 2
Using O(1) time to check whether an integer n is a power of2.ExampleFor n=4, return true;For n=5, return false;ChallengeO(1) time若为2的幂次方,则只存在一位为1. 可以1)统计1的个数 2)与n-1做与原创 2016-02-23 06:00:32 · 523 阅读 · 0 评论 -
[Lintcode]Partition Array by Odd and Even
Partition an integers array into odd number first and even number second.ExampleGiven [1, 2, 3, 4], return [1, 3, 2, 4]ChallengeDo it in-place.双指针,一个指针从左至右寻找偶数,另一个从右至原创 2016-02-23 22:35:27 · 279 阅读 · 0 评论 -
[Lintcode]Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2016-02-23 23:44:44 · 242 阅读 · 0 评论 -
[Lintcode]Permutation Index
Question :Given a permutation which contains no repeated number, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1.原创 2016-02-24 16:56:57 · 411 阅读 · 0 评论 -
[Lintcode]Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.Have you met this qu原创 2016-02-24 17:17:46 · 334 阅读 · 0 评论 -
[Lintcode]Product of Array Exclude Itself
Given an integers array A.Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WITHOUT divide operation.ExampleFor A = [1, 2, 3], return [6, 3, 2].分成两个数组分别储存(1,原创 2016-02-24 17:58:45 · 250 阅读 · 0 评论 -
[Lintcode]Recover Rotated Sorted Array Show result
Given a rotated sorted array, recover it to sorted array in-place.Example[4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]ChallengeIn-place, O(1) extra space and O(n) time.找到数组的旋转点,旋转原创 2016-02-24 21:31:36 · 278 阅读 · 0 评论 -
[Lintcode]Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2016-02-24 22:04:54 · 202 阅读 · 0 评论 -
[Lintcode] Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,原创 2016-02-24 22:54:34 · 180 阅读 · 0 评论