随笔分类 -  算法

摘要:题目:给定一个字符串,例如:abbcdef,最长不重复的子串为bcdef。 kotlin代码实现: 1 fun main() { 2 val str = "abbcde" 3 var longest = "" 4 5 var i = 0 6 while (i < str.length) { 7 va 阅读全文
posted @ 2022-03-15 18:37 -小城- 阅读(21) 评论(0) 推荐(0)
摘要:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。(时间限制:1秒,空间限制:32768K)参考答案: 1 public static boolean find(int [][] ar... 阅读全文
posted @ 2015-09-10 16:20 -小城- 阅读(155) 评论(0) 推荐(0)
摘要:难度:中等给k个字符串,求出他们的最长公共前缀(LCP)样例在 "ABCD" "ABEF" 和 "ACEF" 中, LCP 为 "A"在"ABCDEFG", "ABCEFG", "ABCEFA" 中, LCP 为 "ABC"答案: 1 public class Solution { 2 3... 阅读全文
posted @ 2015-08-31 20:12 -小城- 阅读(303) 评论(0) 推荐(0)
摘要:难度:中等给出两个字符串,找到最长公共子串,并返回其长度。样例给出A=“ABCD”,B=“CBCE”,返回 2。A=“www.lintcode.com code”,B=“www.ninechapter.com code”,返回 9。注意子串的字符应该连续的出现在原字符串中,这与子序列有所不同。答案:... 阅读全文
posted @ 2015-08-31 18:33 -小城- 阅读(237) 评论(1) 推荐(0)
摘要:难度:中等给出一个字符串数组S,找到其中所有的乱序字符串(Anagram)。如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中。样例对于字符串数组["lint","intl","inlt","code"]返回["lint","inlt","intl"]注意所有的字符串... 阅读全文
posted @ 2015-08-29 12:14 -小城- 阅读(315) 评论(0) 推荐(0)
摘要:难度:容易写出一个函数anagram(s, t)去判断两个字符串是否是颠倒字母顺序构成的。样例给出 s="abcd",t="dcab",返回true。S="abcd",T="aabd",返回false。答案: 1 public class Solution { 2 /** 3 * ... 阅读全文
posted @ 2015-08-29 01:26 -小城- 阅读(324) 评论(0) 推荐(0)
摘要:难度:中等Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the t... 阅读全文
posted @ 2015-08-29 00:50 -小城- 阅读(193) 评论(0) 推荐(0)
摘要:难度:中等一个整数数组,除了一个数之外所有数字都出现了3次,找出这个数字来。注意: 你的算法应该是线性运行复杂度,且不能使用额外内存空间。答案:public class Solution { public int singleNumber(int[] nums) { int on... 阅读全文
posted @ 2015-08-27 22:36 -小城- 阅读(189) 评论(0) 推荐(0)
摘要:难度:中等一个整数数组,除了一个数之外所有数字都出现了2次,找出这个数字来。注意: 你的算法应该是线性运行复杂度,且不能使用额外内存空间。答案:public class Solution { public int singleNumber(int[] nums) { int n ... 阅读全文
posted @ 2015-08-27 22:35 -小城- 阅读(143) 评论(0) 推荐(0)
摘要:难度:中等我们把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。参考答案:public class Solution { public int nthUglyNumbe... 阅读全文
posted @ 2015-08-27 02:21 -小城- 阅读(549) 评论(0) 推荐(0)
摘要:难度:中等给定一个从 0, 1, 2, ...., n 包含了n个唯一数字的数组,查找数组中遗漏的那个数字。例如:数组nums=[0, 1, 3],则返回2。答案:public class Solution { public int missingNumber(int[] nums) { ... 阅读全文
posted @ 2015-08-27 00:29 -小城- 阅读(308) 评论(0) 推荐(0)