- 博客(44)
- 资源 (1)
- 收藏
- 关注
原创 [Lintcode]Sort List链表排序
在 O(n log n) 时间复杂度和常数级的空间复杂度下给链表排序。样例给出 1->3->2->null,给它排序变成 1->2->3->null.分析:O(nlogn)时间复杂度排序,并且是链表,可以考虑归并排序。平均复杂度满足条件。/** * Definition for ListNode. * public class ListNode { *
2016-10-31 23:08:41 524
原创 [Lintcode]Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).分析:实现题。注意交换元素时坐标计算public class Solution { /** * @param matrix: A list of lists of i
2016-10-30 17:35:46 321
原创 *[Lintcode]Roman to Integer
Given a roman numeral, convert it to an integer.The answer is guaranteed to be within the range from 1 to 3999.分析:罗马计数法一般都是大小递减。特例只有:IV, IX, XL, XC, CD, CM这几种情况。这里 遇到递增的情况,特殊处理。public
2016-10-30 17:00:02 196
原创 *[Lintcode]Pow(x, n)
Implement pow(x, n).ExamplePow(2.1, 3) = 9.261Pow(0, 1) = 0Pow(1, 0) = 1分析:首先是O(n)解法public class Solution { /** * @param x the base number * @param n the power number
2016-10-30 16:24:02 277
原创 [Lintcode]Permutations
Given a list of numbers, return all possible permutations.ExampleFor nums = [1,2,3], the permutations are:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]分析:递归。
2016-10-30 15:45:12 157
原创 *[Lintcode]Perfect Square
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.ExampleGiven n = 12, return 3 because 12 = 4 + 4 + 4Given n = 1
2016-10-30 11:19:17 201
原创 [Lintcode]Palindrome Partitioning分割回文串
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.ExampleGiven s = "aab", return:[ ["aa","b"],
2016-10-30 09:50:11 241
原创 *[Lintcode]Palindrome Linked List
Implement a function to check if a linked list is a palindrome.分析:用快慢指针找到中点。中点以后倒序排列。然后head和mid两个指针进行比较即可。注意快慢指针条件/** * Definition for singly-linked list. * public class ListNode { *
2016-10-28 16:58:48 213
原创 *[Lintcode]Next Permutation
Given a list of integers, which denote a permutation.Find the next permutation in ascending order.ExampleFor [1,3,2,3], the next permutation is [1,3,3,2]For [4,3,2,1], the next
2016-10-28 10:49:48 254
原创 [Lintcode]N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.
2016-10-27 23:01:04 165
原创 [Lintcode]Minimum Window Substring
Given a string source and a string target, find the minimum window in source which will contain all the characters in target.ExampleFor source = "ADOBECODEBANC", target = "ABC", the minimum wi
2016-10-26 23:18:03 325
原创 [Lintcode]Minimum Adjustment Cost
Given an integer array, adjust each integers so that the difference of every adjacent integers are not greater than a given number target.If the array before adjustment is A, the array after adjus
2016-10-26 21:58:09 271
原创 [Lintcode]Min Stack
Implement a stack with min() function, which will return the smallest number in the stack.It should support push, pop and min operation all in O(1) cost.Examplepush(1)pop() // return 1
2016-10-26 16:53:30 161
原创 [Lintcode]Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list.Analyze and describe its complexity.ExampleGiven lists:[ 2->4->null, null, -1->null],return -1->2->4->null./**
2016-10-26 15:48:42 153
原创 [Lintcode]Maximum Product Subarray乘积最大子序列
Find the contiguous subarray within an array (containing at least one number) which has the largest product.ExampleFor example, given the array [2,3,-2,4], the contiguous subarray [2,3] ha
2016-10-26 14:48:54 200
原创 **[Lintcode] Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.\分析:从每一个点(startPoint)出发,统计到其他点的斜率。在map中存储key=斜率,value=点数量。遇到与startPoint点重合的点,在计算所有点数量时需要加上重合点
2016-10-25 23:27:00 198
原创 **[Lintcode]Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1/k of the size of the array.Find it.ExampleGiven [3,1,2,3,2,3,3,4,4,4] and k=3, retu
2016-10-24 22:54:19 221
原创 *[Lintcode]Majority Number II 主元素 II
Given an array of integers, the majority number is the number that occursmore than 1/3 of the size of the array.Find it.ExampleGiven [1, 2, 1, 2, 1, 3, 3], return 1.分析:与主元素1不同,这道题是
2016-10-23 17:40:25 567
原创 [Lintcode]Lowest Common Ancestor 最近公共祖先
Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes.The lowest common ancestor is the node with largest depth which is the ancestor of both nodes.
2016-10-23 15:59:43 253
原创 [Lintcode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters.ExampleFor example, the longest substring without repeating letters for "abcabcbb" is "abc", which the
2016-10-23 15:05:02 235
原创 **[Lintcode]Longest Palindromic Substring最长回文子串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.ExampleGiven
2016-10-23 11:17:27 375
原创 *[Lintcode]Longest Increasing Subsequence 最长上升子序列
Given a sequence of integers, find the longest increasing subsequence (LIS).You code should return the length of the LIS.ExampleFor [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3For [
2016-10-23 10:00:19 207
原创 *[Lintcode]Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.ExampleGiven [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3,
2016-10-18 22:49:49 253
原创 [Lintcode]Longest Common Substring最长公共子串
Given two strings, find the longest common substring.Return the length of it.分析:算法与最长公共子序列大致相似。二维DP。区别是,字符不同时,res[i][j] = 0;并另一个额外的int记录最大长度。public class Solution { /** * @p
2016-10-18 19:13:26 436
原创 [Lintcode]Longest Common Subsequence 最长公共子序列
Given two strings, find the longest common subsequence (LCS).Your code should return the length of LCS.ExampleFor "ABCD" and "EDCA", the LCS is "A" (or "D", "C"), return 1.For "ABCD"
2016-10-18 18:31:47 1031 1
原创 [Lintcode]Letter Combinations of a Phone Number
Given a digit string excluded 01, return all possible letter combinations that the number could represent.A mapping of digit to letters just like on the telephone buttonsExampleGiv
2016-10-16 17:15:08 287
原创 *[Lintcode]Largest Number 最大数
Given a list of non negative integers, arrange them such that they form the largest number.ExampleGiven [1, 20, 23, 4, 8], the largest formed number is 8423201.如果不想重写排序算法的话,使用Arrays.so
2016-10-16 15:27:35 203
原创 **[Lintcode]Kth Largest Element
Find K-th largest element in an array. NoticeYou can swap elements in the arrayHave you met this question in a real interview? YesExampleIn array [9,3,2,4,8], t
2016-10-12 22:44:39 179
原创 [Lintcode]跳跃游戏 II
给出一个非负整数数组,你最初定位在数组的第一个位置。数组中的每个元素代表你在那个位置可以跳跃的最大长度。 你的目标是使用最少的跳跃次数到达数组的最后一个位置。样例给出数组A = [2,3,1,1,4],最少到达数组最后一个位置的跳跃次数是2(从数组下标0跳一步到数组下标1,然后跳3步到数组的最后一个位置,一共跳跃2次)分析,一维DP。re
2016-10-12 10:45:06 473
原创 [Lintcode]Jump Game跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine i
2016-10-11 22:21:08 231
原创 *[Lintcode]Interval Sum区间最小数
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the sum number between index s
2016-10-11 17:41:48 314
原创 *[Lintcode]Interval Minimum Number 区间最小数
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the minimum number between ind
2016-10-11 14:07:16 251
原创 [Lintcode]Intersection of Two Linked Lists 两个链表的交叉
Write a program to find the node at which the intersection of two singly linked lists begins.ExampleThe following two linked lists:A: a1 → a2 ↘
2016-10-11 12:43:24 251
原创 [Lintcode] Interleaving String 交叉字符串
Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1and s2.Have you met this question in a real interview? YesExampleFor s1 = "aabcc", s2
2016-10-10 23:23:34 806
原创 *[Lintcode]Interleaving Positive and Negative Numbers 交错正负数
Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers.ExampleGiven [-1, -2, -3, 4, 5, 6], after re-range, it will be [-1, 5, -2, 4
2016-10-09 22:54:20 231
原创 *[lintcode] Integer to Roman 整数转罗马数字
Given an integer, convert it to a roman numeral.The number is guaranteed to be within the range from 1 to 3999.分析:前提先掌握罗马数字规律。我们将3999一下可能代表数字的所有罗马字母放入数组,从高到低依次去除n。注意900,400,90,40,9,4由于涉及到变位,为了
2016-10-08 22:51:02 406
原创 [Lintcode]Implement Trie 字典树
Implement a trie with insert, search, and startsWith methods.Exampleinsert("lintcode")search("code") // return falsestartsWith("lint") // return truestartsWith("linterror") // return falsein
2016-10-07 18:00:31 188
原创 [Lintcode]Implement Queue by Two Stacks
As the title described, you should only use two stacks to implement a queue's actions.The queue should support push(element), pop() and top()where pop is pop the first(a.k.a front) element in th
2016-10-07 17:18:53 187
原创 **[Lintcode] House Rober III 打劫房屋 III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour
2016-10-07 17:04:11 473
原创 ***[Lintcode]House Robber II
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a ci
2016-10-06 18:00:20 206
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人