随笔分类 - LintCode
LintCode刷刷刷
摘要:丑数设计一个算法,找出只含素因子3,5,7的第k大的数。符合条件的数如:3,5,7,9,15......您在真实的面试中是否遇到过这个题?Yes样例如果k=4, 返回9挑战要求时间复杂度为O(nlogn)或者O(n) 1 import java.util.Queue; 2 import java.u...
阅读全文
摘要:木材加工有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为k。当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度。样例有3根木头[232, 124, 456],k=7, 最大长度为114.注意木头长度的单位是厘米。原木的长度都是正整数,我们要求...
阅读全文
摘要:Word LadderGiven two words (startandend), and a dictionary, find the length of shortest transformation sequence fromstarttoend, such that:Only one let...
阅读全文
摘要:Container With Most WaterGiven n non-negative integers a1, a2, ..., an, where each represents a point at coordinate(i, ai).nvertical lines are drawn s...
阅读全文
摘要:Single Number IIGiven3*n + 1numbers, every numbers occurs triple times except one, find it.ExampleGiven[1,1,2,3,3,3,2,2,4,1]return4ChallengeOne-pass, ...
阅读全文
摘要:Spiral MatrixGiven a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.ExampleGiven the following matrix:[ [ 1,...
阅读全文
摘要:Continuous Subarray Sum IIGiven an circular integer array (the next element of the last element is the first element), find a continuous subarray in i...
阅读全文
摘要:颜色分类给定一个包含红,白,蓝且长度为n的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红、白、蓝的顺序进行排序。我们可以使用整数0,1和2分别代表红,白,蓝。样例注意不能使用代码库中的排序函数来解决这个问题说明一个相当直接的解决方案是使用计数排序扫描2遍的算法。首先,迭代数组计算0,1,2出...
阅读全文
摘要:验证二叉查找树给定一个二叉树,判断它是否是合法的二叉查找树(BST)一棵BST定义为:节点的左子树中的值要严格小于该节点的值。节点的右子树中的值要严格大于该节点的值。左右子树也必须是二叉查找树。样例一个例子: 2 / \1 4 / \ 3 5上述这棵二叉树序列化为{2,1,4,#,#...
阅读全文
摘要:Matrix Zigzag TraversalGiven a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in ZigZag-order.Have you met this question in ...
阅读全文
摘要:爬楼梯假设你正在爬楼梯,需要n步你才能到达顶部。但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部?样例比如n=3,1+1+1=1+2=2+1=3,共有3中不同的方法返回 3用递归又超时了。。于是又换了DP,dp并不熟悉,于是又搞了好久。首先向右是跳一格,向下是跳两格,dp[j]是到达(i...
阅读全文
摘要:Valid PalindromeGiven a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.Example"A man, a plan, a ...
阅读全文
摘要:最大子数组给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。样例给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6注意子数组最少包含一个数挑战要求时间复杂度为O(n)若当前和小于0,那么加到下一个数上必然会使和减小,此时抛弃这个和重...
阅读全文
摘要:子树有两个不同大小的二进制树:T1有上百万的节点;T2有好几百的节点。请设计一种算法,判定T2是否为T1的子树。样例下面的例子中 T2 是 T1 的子树: 1 3 / \ / T1 = 2 3 T2 = 4...
阅读全文
摘要:合并区间给出若干闭合区间,合并所有重叠的部分。样例给出的区间列表 => 合并后的区间列表:[ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10], ...
阅读全文
摘要:单例单例是最为最常见的设计模式之一。对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例。例如,对于 class Mouse (不是动物的mouse哦),我们应将其设计为 singleton 模式。你的任务是设计一个getInstance方法,对于给定的类,每次调用g...
阅读全文

浙公网安备 33010602011771号