
lintcode
zyaphone
这个作者很懒,什么都没留下…
展开
-
LintCode:二叉树的前序遍历、中序遍历、后序遍历
二叉树的前序遍历39% 通过 给出一棵二叉树,返回其节点值的前序遍历。您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵二叉树 {1,#,2,3},1 \ 2 / 3 返回 [1,2,3].挑战 你能使用非递归实现么?/*暂时只会用递归*//** * Definition of TreeNode: * class TreeNode {原创 2015-10-08 23:12:59 · 386 阅读 · 0 评论 -
Lintcode: 颜色分类
Lintcode: 颜色分类给定一个包含红,白,蓝且长度为n的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红、白、蓝的顺序进行排序。我们可以使用整数0,1和2分别代表红,白,蓝。您在真实的面试中是否遇到过这个题? Yes 样例 注意 不能使用代码库中的排序函数来解决这个问题说明 一个相当直接的解决方案是使用计数排序扫描2遍的算法。首先,迭代数组计算0,1,2出现的次数,然后依次用0,原创 2015-10-08 20:47:14 · 459 阅读 · 0 评论 -
LintCode: 最大子数组
容易 最大子数组 查看运行结果 37% 通过 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6注意 子数组最少包含一个数挑战 要求时间复杂度为O(n)/*动态规划:状态方程:ans[0] = nums[0]ans原创 2015-10-09 13:51:08 · 667 阅读 · 0 评论 -
LintCode:最小路径和
最小路径和 给定一个只含非负整数的m*n网格,找到一条从左上角到右下角的可以使数字和最小的路径。 LintCode:最小路径和思路一:万能的枚举m行,n列的矩阵,从左上角走到右下角,需要向下移动(m-1)步,向右移动(n-1)步,共(m+n-2)步,则路径总条数为C(m+n-2, m-1)步。m,n较小时可行,较大时不可行。思路二: 动态规划dp[i][j] 表示从左上到达grid[i原创 2015-10-10 13:36:57 · 719 阅读 · 0 评论 -
LintCode : 加一
LintCode : 加一给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组。该数字按照大小进行排列,最大的数在列表的最前面。先把数组组装成字符串,再把字符串转成数组,然后加1,再转成字符串,最后转成数组,有点麻烦。public class Solution { /** * @param digits a number represented as an ar原创 2015-10-26 21:52:02 · 960 阅读 · 0 评论 -
LintCode:二叉树的最小深度
Lintcode : 二叉树的最小深度给定一个二叉树,找出其最小深度。二叉树的最小深度为根节点到最近叶子节点的距离。与二叉树的最大深度不同,不能单纯地使用递归,因为二叉树的深度必须是根结点到叶子结点的距离,不能单纯的比较左右子树的递归结果返回较小值,因为对于有单个孩子为空的节点,为空的孩子会返回0,但这个节点并非叶子节点,故返回的结果是错误的。因此,当发现当前处理的节点有单个孩子是空时,返回一个极大原创 2015-10-11 11:18:42 · 3895 阅读 · 0 评论 -
LintCode : 第一个错误的代码版本
LintCode : 第一个错误的代码版本代码库的版本号是从 1 到 n 的整数。某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错。请找出第一个错误的版本号。你可以通过 isBadVersion 的接口来判断版本号 version 是否在单元测试中出错,具体接口详情和调用方法请见代码的注释部分。二分查找。/** * class VersionControl {原创 2015-10-11 14:22:55 · 421 阅读 · 0 评论 -
LintCode : 搜索旋转排序数组
LintCode : 搜索旋转排序数组假设有一个排序的按未知的旋转轴旋转的数组(比如,0 1 2 4 5 6 7 可能成为4 5 6 7 0 1 2)。给定一个目标值进行搜索,如果在数组中找到目标值返回数组中的索引位置,否则返回-1。你可以假设数组中不存在重复的元素。方案一:遍历所有元素没有什么技术含量,就是遍历数组的所有元素, 时间复杂度O(n)。class Solution { /**原创 2015-10-11 15:34:41 · 363 阅读 · 0 评论 -
LintCode : 搜索旋转排序数组 II
LintCode : 搜索旋转排序数组 II跟进“搜索旋转排序数组”,假如有重复元素又将如何?是否会影响运行时间复杂度?如何影响?为何会影响?写出一个函数判断给定的目标值是否出现在数组中。方案一遍历所有元素,时间复杂度O(n).class Solution { /** * param A : an integer ratated sorted array and duplica原创 2015-10-11 15:37:32 · 366 阅读 · 0 评论 -
LintCode:二叉树的最大深度
LintCode:二叉树的最大深度给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的距离。显然,这是一个典型的递归。/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val)原创 2015-10-10 23:10:05 · 1044 阅读 · 0 评论 -
LintCode : 搜索插入位置
LintCode : 搜索插入位置给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。你可以假设在数组中无重复元素。二分法。class Solution { /** * param A : an integer sorted array * param target : an integer to be inse原创 2015-10-11 16:06:02 · 384 阅读 · 0 评论 -
LintCode : 最长公共子串
LintCode : 最长公共子串给出两个字符串,找到最长公共子串,并返回其长度。与LintCode上的另一题类似, 不同的是“串”是连续的,而“序列”不要求连续。状态方程:f[i][j] = f[i-1][j-1] + 1; (A[i] = B[i])时 f[i][j] = 0; (A[i] != B[i])时class Solution {public: /**原创 2015-10-12 16:30:00 · 376 阅读 · 0 评论 -
LintCode : Maximal Square
# LintCode : Maximal SquareGiven a 2D binary matrix filled with 0’s and 1’s, find the largest square containing all 1’s and return its area.动态规划:动态方程 dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][原创 2015-10-26 16:23:54 · 302 阅读 · 0 评论 -
LintCode : 有效回文串
LintCode : 有效回文串给定一个字符串,判断其是否为一个回文串。只包含字母和数字,忽略大小写。双指针public class Solution { /** * @param s A string * @return Whether the string is a valid palindrome */ public boolean isPali原创 2015-10-26 17:52:51 · 428 阅读 · 0 评论 -
LintCode : 最长公共子序列
LintCode : 最长公共子序列给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度。动态规划。大神July讲的很详细,请大家移步这里。class Solution {public: /** * @param A, B: Two strings. * @return: The length of longest common subsequence of原创 2015-10-12 16:01:21 · 645 阅读 · 0 评论 -
LintCode : 搜索区间
LintCode : 搜索区间给定一个包含 n 个整数的排序数组,找出给定目标值 target 的起始和结束位置。如果目标值不在数组中,则返回[-1, -1]方案一:指定一高一低两个指针,低指针向后遍历,高指针向前遍历,最终低指针指向起始位置,高指针指向结束位置。时间复杂度O(n)。class Solution { /** *@param A : an integer sort原创 2015-10-11 17:19:48 · 330 阅读 · 0 评论 -
LintCode : 背包问题 II
LintCode : 背包问题 II给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大?您在真实的面试中是否遇到过这个题?典型的01背包问题,动态规划解决。状态转移方程:ans[i][j] = max ( ans[i-1][j-A[i-1]] + V[i-1], ans[i-1][j-1] ) 当(j >= V[i])时。ans[i][j] =原创 2015-10-11 22:40:18 · 655 阅读 · 0 评论 -
LintCode : 排颜色 II
LintCode : 排颜色 II给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,…k的顺序进行排序。一个相当直接的解决方案是使用计数排序扫描2遍的算法。这样你会花费O(k)的额外空间。你否能在不使用额外空间的情况下完成?暂时还没想到,只能用map解决,慢慢想吧。。。class Solution{public: /*原创 2015-10-12 17:55:17 · 538 阅读 · 0 评论 -
LintCode : 颠倒整数
LintCode : 颠倒整数将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。class Solution: # @param {int} n the integer to be reversed # @return {int} the reversed integer def reverseInteger(self, n):原创 2015-10-12 14:18:16 · 397 阅读 · 0 评论 -
Lintcode打劫房屋
Lintcode 打劫房屋典型的动态规划,方程: f(n) = max{L(n) + f(n-2), f(n-1)}python代码class Solution: # @param A: a list of non-negative integers. # return: an integer def houseRobber(self, A): # writ原创 2016-03-17 13:43:44 · 398 阅读 · 0 评论 -
Lintcode:wiggle-sort
Lintcode:wiggle-sort没啥特别的,另一种形式的冒泡排序。 先把答案做出来,下一步想想怎么优化,留个坑先。class Solution(object): """ @param {int[]} nums a list of integer @return nothing, modify nums in-place instead """ def原创 2016-03-17 14:28:12 · 338 阅读 · 0 评论 -
LintCode:快乐数
LintCode:快乐数Python代码class Solution: # @param {int} n an integer # @return {boolean} true if this is a happy number or false n_list = [] def isHappy(self, n): # Write your code h原创 2016-03-22 15:54:24 · 532 阅读 · 0 评论 -
LintCode:在O(1)时间复杂度删除链表节点
LintCode:在O(1)时间复杂度删除链表节点/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next原创 2016-03-22 15:57:20 · 330 阅读 · 0 评论 -
LintCode:两两交换链表中的节点
LintCode:两两交换链表中的节点/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public原创 2016-03-22 16:28:18 · 1679 阅读 · 0 评论 -
LintCode:合并两个排序链表
LintCode:合并两个排序链表/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL;原创 2016-03-22 16:59:29 · 356 阅读 · 0 评论 -
Lintcode:买卖股票的最佳时机
Lintcode:买卖股票的最佳时机class Solution: """ @param prices: Given an integer array @return: Maximum profit """ def maxProfit(self, prices): # write your code here if(len(pr原创 2016-04-07 15:47:40 · 382 阅读 · 0 评论 -
LintCode: 带环链表 II
LintCode: 带环链表 II"""Definition of ListNodeclass ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next"""class Solution: """ @param head: T原创 2016-04-25 13:42:48 · 300 阅读 · 0 评论 -
LintCode:装最多水的容器
LintCode:装最多水的容器class Solution: # @param heights: a list of integers # @return: an integer def maxArea(self, heights): # write your code here if len(heights) == 0:原创 2016-04-25 20:36:53 · 439 阅读 · 0 评论 -
LintCode:Ugly Number
LintCode:Ugly Numberclass Solution: # @param {int} num an integer # @return {boolean} true if num is an ugly number or false def isUgly(self, num): # Write your code here if原创 2016-04-25 20:49:10 · 319 阅读 · 0 评论 -
LintCode:Paint House
LintCode:Paint Houseclass Solution: # @param {int[][]} costs n x 3 cost matrix # @return {int} an integer, the minimum cost to paint all houses def minCost(self, costs): # Write you原创 2016-04-25 21:56:11 · 405 阅读 · 0 评论 -
LintCode:Left Pad
LintCode:Left Pad这里写链接内容class StringUtils: # @param {string} originalStr the string we want to append to # @param {int} size the target length of the string # @param {string} padChar the ch原创 2016-04-25 23:11:52 · 778 阅读 · 0 评论 -
LintCode: Paint House II
LintCode: Paint House II最简单的穷举法,可惜超时了,先留个坑~class Solution: # @param {int[][]} costs n x k cost matrix # @return {int} an integer, the minimum cost to paint all houses def minCostII(self, co原创 2016-04-25 23:50:49 · 537 阅读 · 0 评论 -
LintCode:等价二叉树
LintCode:等价二叉树如果一棵树前、中、后序遍历结果都一样的话肯定就是等价的了。"""Definition of TreeNode:class TreeNode: def __init__(self, val): this.val = val this.left, this.right = None, None"""class Solution:原创 2016-04-26 00:27:12 · 789 阅读 · 0 评论 -
LintCode:最长上升连续子序列
LintCode:最长上升连续子序列class Solution: # @param {int[]} A an array of Integer # @return {int} an integer def longestIncreasingContinuousSubsequence(self, A): # Write your code here原创 2016-04-26 10:35:38 · 496 阅读 · 0 评论 -
LintCode:最长回文子串
LintCode:最长回文子串 需要引入一个二维数组M[i]来记录字符串与反转后的字符串的重复状态,以字符串abcdzdcab 为例,M[i]如下图所示: 然后只需要找到M[i]上最长的连续1的个数就是最长回文字符串的长度,记住位置,再在原来的字符串上截取一可以了。class Solution: # @param {string} s input string # @retu原创 2016-04-26 13:55:09 · 479 阅读 · 0 评论 -
LintCode:最长无重复字符的子串
LintCode:最长无重复字符的子串class Solution: # @param s: a string # @return: an integer def lengthOfLongestSubstring(self, s): # write your code here L = [] max_len = 0原创 2016-04-26 20:56:25 · 270 阅读 · 0 评论 -
LintCode: 合并排序数组
LintCode: 合并排序数组class Solution: #@param A and B: sorted integer array A and B. #@return: A new sorted integer array def mergeSortedArray(self, A, B): # write your code here原创 2016-04-26 21:39:34 · 269 阅读 · 0 评论 -
LintCode:平衡二叉树
LintCode:平衡二叉树"""Definition of TreeNode:class TreeNode: def __init__(self, val): self.val = val self.left, self.right = None, None"""class Solution: """ @param root: The原创 2016-04-26 22:42:48 · 703 阅读 · 0 评论 -
LintCode:数组划分
LintCode:数组划分一前一后两根指针,当前面的指针大于k且后指针小于k时,交换两个指针的值,走到两根指针相遇。class Solution: """ @param nums: The integer array you should partition @param k: As description @return: The index after parti原创 2016-04-26 23:39:36 · 475 阅读 · 0 评论 -
LintCode:字符大小写排序
LintCode:字符大小写排序双指针。class Solution: """ @param chars: The letters array you should sort. """ def sortLetters(self, chars): # write your code here m = 0 n = len(c原创 2016-04-26 23:46:20 · 471 阅读 · 0 评论