
LintCode
文章平均质量分 79
lighthear
这个作者很懒,什么都没留下…
展开
-
13. strStr
Description对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。Clarification在面试中我是否需要实现KMP算法?不需要,当这种问题出现在面试中时,面试官很可能只是想要测试一下你的基础应用能力。当然你需要先跟面试官确认清楚要怎么实现这个题。Exampl...原创 2018-02-26 20:11:57 · 117 阅读 · 0 评论 -
130. Heapify-堆化(siftup & siftdown版本)
Description给出一个整数数组,堆化操作就是把它变成一个最小堆数组。对于堆数组A,A[0]是堆的根,并对于每个A[i],A [i * 2 + 1]是A[i]的左儿子并且A[i * 2 + 2]是A[i]的右儿子。说明什么是堆?堆是一种数据结构,它通常有三种方法:push, pop 和 top。其中,“push”添加新的元素进入堆,“pop”删除堆中最小/最大元素,“top”返回堆中最小/最...原创 2018-04-14 23:21:17 · 6358 阅读 · 1 评论 -
143. Sort Colors II - 排颜色 II(rainbowSort彩虹排序)
Description给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序。 注意事项You are not suppose to use the library's sort function for this problem.k <= n样例给出colors=[3, 2, 2, 1, 4],k=4,...原创 2018-04-02 20:50:52 · 5856 阅读 · 1 评论 -
615. Course Schedule-课程表(拓扑排序)
Description现在你总共有 n 门课需要选,记为 0 到 n - 1.一些课程在修之前需要先修另外的一些课程,比如要学习课程 0 你需要先学习课程 1 ,表示为[0,1]给定n门课以及他们的先决条件,判断是否可能完成所有课程?样例给定 n = 2,先决条件为 [[1,0]] 返回 true给定 n = 2,先决条件为 [[1,0],[0,1]] 返回 falseSolution算法思路:已...原创 2018-04-10 10:01:40 · 1447 阅读 · 0 评论 -
137. Clone Graph——克隆图(BFS宽度优先搜索)
Description克隆一张无向图,图中的每个节点包含一个 label 和一个列表 neighbors。数据中如何表示一个无向图?http://www.lintcode.com/help/graph/你的程序需要返回一个经过深度拷贝的新图。这个新图和原图具有同样的结构,并且对新图的任何改动不会对原图造成任何影响。样例比如,序列化图 {0,1,2#1,2#2,2} 共有三个节点, 因此包含两个个分...原创 2018-03-29 07:51:30 · 409 阅读 · 0 评论 -
127. Topological Sorting——拓扑排序(BFS)
DescriptionGiven an directed graph, a topological order of the graph nodes is defined as follow:For each directed edge A -> B in graph, A must before B in the order list.The first node in the order...原创 2018-03-29 08:48:56 · 1134 阅读 · 0 评论 -
814. Shortest Path in Undirected Graph-无向图最短路径(双向宽度优先搜索算法)
DescriptionGive an undirected graph, in which each edge's length is 1, and give two nodes from the graph. We need to find the length of the shortest path between the given twonodes.样例Given graph = {1,...原创 2018-04-04 21:34:38 · 837 阅读 · 0 评论 -
120. Word Ladde——单词接龙(BFS分层遍历)
Description给出两个单词(start和end)和一个字典,找到从start到end的最短转换序列比如:每次只能改变一个字母。变换过程中的中间单词必须在字典中出现。 注意事项如果没有转换序列则返回0。所有单词具有相同的长度。所有单词都只包含小写字母。Given two words (start and end), and a dictionary, find the length of s...原创 2018-03-29 16:21:18 · 1177 阅读 · 0 评论 -
7. Binary Tree Serialization-二叉树的序列化和反序列化(BFS)
Description设计一个算法,并编写代码来序列化和反序列化二叉树。将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”。如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉树序列化为一个字符串,并且可以将字符串反序列化为原来的树结构。 注意事项There is no limit of how you deserialize or serialize a b...原创 2018-04-05 12:25:38 · 640 阅读 · 0 评论 -
433. Number of Islands-岛屿的个数
Description给一个01矩阵,求不同的岛屿的个数。0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。样例在矩阵:[ [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]]中有 3 个岛.Solution算法思路:1....原创 2018-04-07 11:43:42 · 589 阅读 · 0 评论 -
900. Closest Binary Search Tree Value
DescriptionGiven a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. 注意事项Given target value is a floating point.You are guaranteed to have only ...原创 2018-04-19 17:10:43 · 423 阅读 · 0 评论 -
86. Binary Search Tree Iterator-二叉查找树迭代器(非递归的二叉树中序遍历)
Description设计实现一个带有下列属性的二叉查找树的迭代器:元素按照递增的顺序被访问(比如中序遍历)next()和hasNext()的询问操作要求均摊时间复杂度是O(1)样例对于下列二叉查找树,使用迭代器进行中序遍历的结果为 [1, 6, 10, 11, 12] 10 / \1 11 \ \ 6 12挑战 额外空间复杂度是O(h),其...原创 2018-04-21 21:20:53 · 986 阅读 · 0 评论 -
382. Triangle Count-三角形计数
Description给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形?Given an array of integers, how many three numbers can be found in the array, so that we can build an triangle whose three edges le...原创 2018-03-30 23:02:42 · 521 阅读 · 0 评论 -
57. 3Sum-三数之和
Description给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。 注意事项在三元组(a, b, c),要求a <= b <= c。结果不能包含重复的三元组。Given an array S of n integers, are there elements a, b, c in S such that a + b + ...原创 2018-03-30 21:46:22 · 326 阅读 · 0 评论 -
200. Longest Palindromic Substring-最长回文子串
Description给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串。样例给出字符串 "abcdzdcab",它的最长回文子串为 "cdzdc"。挑战 O(n2) 时间复杂度的算法是可以接受的,如果你能用 O(n) 的算法那自然更好。Solution这道题一共有五种解法:Manacher's Algorithm - O(n) ---...原创 2018-03-18 17:11:57 · 804 阅读 · 0 评论 -
457. Classical Binary Search 模板解法
DescriptionFind any position of a target number in a sorted array. Return -1 if target does not exist.ExampleGiven[1, 2, 2, 4, 5, 5].For target =2, return 1 or 2.For target =5, return 4 or 5.For ta...原创 2018-02-26 23:34:42 · 338 阅读 · 1 评论 -
464. Sort Integers II (Quick Sort & Merge Sort)
DescriptionGiven an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(nlogn) algorithm.ExampleGiven [3, 2, 1, 4, 5], return [1, 2, 3, 4, 5].Tags Quick Sort Sort...原创 2018-03-07 08:41:07 · 321 阅读 · 0 评论 -
74. First Bad Version(二分法)
DescriptionThe code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the ...原创 2018-02-27 19:19:15 · 162 阅读 · 0 评论 -
在大数组中查找 · Search in a Big Sorted Array(二分法)
Description给一个按照升序排序的正整数数组。这个数组很大以至于你只能通过固定的接口 ArrayReader.get(k) 来访问第k个数。(或者C++里是ArrayReader->get(k)),并且你也没有办法得知这个数组有多大。找到给出的整数target第一次出现的位置。你的算法需要在O(logk)的时间复杂度内完成,k为target第一次出现的位置的下标。如果找不到targe...原创 2018-02-27 22:15:07 · 1159 阅读 · 0 评论 -
159. Find Minimum in Rotated Sorted Array(二分法)
DescriptionSuppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element. NoticeYou may assume no duplicate exists...原创 2018-02-27 23:00:19 · 237 阅读 · 0 评论 -
75. Find Peak Element(二分法)
LintCodeDescriptionThere is an integer array which has the following features:The numbers in adjacent positions are different.A[0] < A[1] && A[A.length - 2] > A[A.length - 1].We define a...原创 2018-02-28 10:33:38 · 448 阅读 · 0 评论 -
69. Binary Tree Level Order Traversal - 二叉树的层次遍历
DescriptionGiven a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).ExampleGiven binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \...原创 2018-03-14 18:03:56 · 361 阅读 · 1 评论 -
17. Subsets
Given a set of distinct integers, return all possible subsets. NoticeElements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.ExampleIf S = [1,2,3], a s...原创 2018-02-21 00:08:36 · 215 阅读 · 0 评论 -
18. Subsets II
DescriptionGiven a list of numbers that may has duplicate numbers, return all possible subsets NoticeEach element in a subset must be in non-descending order.The ordering between two subsets is free.T...原创 2018-02-21 21:20:28 · 200 阅读 · 0 评论 -
437. Copy Books
DescriptionGiven n books and the ith book has A[i] pages. You are given k people to copy the n books.n books list in a row and each person can claim a continous range of the n books. For example one c...原创 2018-03-01 19:26:52 · 684 阅读 · 0 评论 -
strStr II - Rabin Karp
DescriptionImplement strStr function in O(n + m) time.strStr return the first index of the target string in a source string. The length of the target string is m and the length of the source string is...原创 2018-02-23 00:14:15 · 274 阅读 · 0 评论 -
67. Binary Tree Inorder Traversal-二叉树的中序遍历(递归与非递归法)
Description给出一棵二叉树,返回其中序遍历样例给出二叉树 {1,#,2,3}, 1 \ 2 / 3返回 [1,3,2].挑战 你能使用非递归算法来实现么?Solution算法思路:遍历法(递归)分治法(递归)非递归法实现中序遍历的通用版本(非递归) 利用 stack 进行 Binary Tree Iterator。stack 中保存一路走到当前节...原创 2018-04-21 21:56:30 · 609 阅读 · 0 评论