
LintCode
wutingyehe
Just for fun
展开
-
【LintCode】Search Insert Position 搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。 你可以假设在数组中无重复元素。样例 [1,3,5,6],5 → 2 [1,3,5,6],2 → 1 [1,3,5,6], 7 → 4 [1,3,5,6],0 → 0public class Solution { /** * param A : an integer原创 2015-07-15 22:07:17 · 1909 阅读 · 0 评论 -
【LintCode】Number of Island 岛屿的数目
给一个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 个岛.//广度优先搜索,时间复杂度原创 2015-08-15 17:12:10 · 1915 阅读 · 0 评论 -
【LintCode】在无向图中找出各极大连通子图 Find the Connected Component in the Undirected Graph
Find the number connected component in the undirected graph. Each node in the graph contains a label and a list of its neighbors. (a connected component (or just component) of an undirected graph is a原创 2015-08-16 15:01:27 · 3168 阅读 · 0 评论 -
[LintCode]Segment Tree Build 构造线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end denote an segment / interval. start and end are both integers, they should be assigned in following rule原创 2015-09-06 11:18:55 · 616 阅读 · 0 评论 -
[LintCode]Segment Tree Query 线段树的查询
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding SegmentTree, each node stores an extra attribute max to denote the maximum number in the interval of原创 2015-09-06 15:01:40 · 901 阅读 · 0 评论 -
[Lintcode] Remove Linked List Elements 删除链表中的元素
删除链表中等于给定值val的所有节点。样例 给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。Remove all elements from a linked list of integers that have value val.Example Given 1->2->3->3->4->5->3, val = 3,原创 2016-02-03 19:36:29 · 4190 阅读 · 0 评论 -
[Lintcode]用栈实现队列
正如标题所述,你需要使用两个栈来实现队列的一些操作。 队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素。 pop和top方法都应该返回第一个元素的值。样例 比如push(1), pop(), push(2), push(3), top(), pop(),你应该返回1,2和2挑战 仅使用两个栈来实现它,不使用任何其他数据结构,pu原创 2016-02-04 16:39:54 · 1057 阅读 · 3 评论 -
[LintCode]搜索二维矩阵 II
写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数。 这个矩阵具有以下特性: 每行中的整数从左到右是排序的。 每一列的整数从上到下是排序的。 在每一行或每一列中没有重复的整数。样例 考虑下列矩阵: [ [1, 3, 5, 7], [2, 4, 7, 8], [3, 5, 9, 10] ] 给出target = 3,返回 2挑战 要求O(m+原创 2016-02-04 17:45:14 · 902 阅读 · 0 评论 -
[LintCode] 搜索旋转排序数组 Search in Rotated Sorted Array
假设有一个排序的按未知的旋转轴旋转的数组(比如,0 1 2 4 5 6 7 可能成为4 5 6 7 0 1 2)。给定一个目标值进行搜索,如果在数组中找到目标值返回数组中的索引位置,否则返回-1。 你可以假设数组中不存在重复的元素。样例 给出[4, 5, 1, 2, 3]和target=1,返回 2 给出[4, 5, 1, 2, 3]和target=0,返回 -1public class So原创 2016-04-06 10:43:01 · 471 阅读 · 0 评论 -
[Lintcode] 搜索旋转排序数组 II Search in Rotated Sorted Array II
跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中。样例 给出[3,4,4,5,7,0,1,2]和target=4,返回 true Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed原创 2016-04-06 12:33:40 · 667 阅读 · 0 评论 -
[LintCode] 落单的数II Single Number II
给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字。 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度,O(1)的额外空间复杂度Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Example Given [1,原创 2016-04-07 09:20:38 · 610 阅读 · 0 评论 -
【LintCode】Maximal Square
Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing all 1’s and return its area.样例 For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0原创 2015-07-31 15:29:48 · 891 阅读 · 0 评论 -
【LintCode】最多有多少个点在一条直线上
给出二维平面上的n个点,求最多有多少点在同一条直线上。样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3)。 一条直线上的点最多有3个。/** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Po原创 2015-07-30 16:37:52 · 2244 阅读 · 0 评论 -
【LintCode】 Linked List Cycle 带环链表
给定一个链表,判断它是否有环。样例 给出 -21->10->4->5, 尾部链到序号为1的结点,返回 true挑战 不要使用额外的空间/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { *原创 2015-07-15 22:40:20 · 574 阅读 · 0 评论 -
【LintCode】 Find Minimum in Rotated Sorted Array 寻找旋转排序数组中的最小值
假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。 你需要找到其中最小的元素。 你可以假设数组中不存在重复的元素。样例 给出[4,5,6,7,0,1,2] 返回 0public class Solution { /** * @param num: a rotated sorted array * @r原创 2015-07-15 22:53:35 · 572 阅读 · 0 评论 -
【LintCode】 Best Time to Buy and Sell Stock 买卖股票的最佳时机
假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。样例 给出一个数组样例 [3,2,3,1,2], 返回 1 public class Solution { /** * @param prices: Given an integer array * @return: Maximu原创 2015-07-15 23:11:06 · 498 阅读 · 0 评论 -
【LintCode】Product of Array Exclude Itself 数组剔除元素后的乘积
给定一个整数数组A。 定义B[i] = A[0] * … * A[i-1] * A[i+1] * … * A[n-1], 计算B的时候请不要使用除法。样例 给出A=[1, 2, 3],返回 B为[6, 3, 2]public class Solution { /** * @param A: Given an integers array A * @return: A原创 2015-07-15 12:04:08 · 846 阅读 · 0 评论 -
【LintCode】Recover Rotated Sorted Array 恢复旋转排序数组
给定一个旋转排序数组,在原地恢复其排序。样例 [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]挑战 使用O(1)的额外空间和O(n)时间复杂度说明 什么是旋转数组? 比如,原始数组为[1,2,3,4], 则其旋转数组可以是[1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3]public class Solution { /**原创 2015-07-15 22:01:15 · 1232 阅读 · 0 评论 -
【LintCode】Insert Intervals 插入区间
给出一个无重叠的按照区间起始端点排序的区间列表。 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。样例 插入区间[2, 5] 到 [[1,2], [5,9]],我们得到 [[1,9]]。 插入区间[3, 4] 到 [[1,2], [5,9]],我们得到 [[1,2], [3,4], [5,9]]。/** * Definition of Inte原创 2015-07-16 10:56:53 · 1352 阅读 · 0 评论 -
【LintCode】 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II
假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格。设计一个算法来找到最大的利润。你可以完成尽可能多的交易(多次买卖股票)。然而,你不能同时参与多个交易(你必须在再次购买前出售股票)。样例 给出一个数组样例[2,1,2,0,1], 返回 2分析: 由于不限买卖次数,因此当相邻的第二天的价格高于第一天时即可进行买卖获利。class Solution { /** * @p原创 2015-07-16 16:08:23 · 776 阅读 · 0 评论 -
【LintCode】 Backpack II 背包问题II
给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大?样例 对于物品体积[2, 3, 5, 7]和对应的价值[1, 5, 2, 4], 假设背包大小为10的话,最大能够装入的价值为9。注意 A[i], V[i], n, m均为整数。你不能将物品进行切分。你所挑选的物品总体积需要小于等于给定的m。public class Solution {原创 2015-07-16 15:36:31 · 1068 阅读 · 0 评论 -
【LintCode】 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 III
假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格。设计一个算法来找到最大的利润。你最多可以完成两笔交易。样例 给出一个样例数组 [4,4,6,1,1,4,2,5], 返回 6注意 你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票)class Solution { /** * @param prices: Given an integer array原创 2015-07-30 10:05:28 · 739 阅读 · 0 评论 -
【LintCode】Evaluate Reverse Polish Notation 逆波兰表达式求值
求逆波兰表达式的值。 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰计数表达。样例 [“2”, “1”, “+”, “3”, ““] -> ((2 + 1) 3) -> 9 [“4”, “13”, “5”, “/”, “+”] -> (4 + (13 / 5)) -> 6 说明 什么是逆波兰表达式? http://en.wi原创 2015-07-30 17:12:43 · 563 阅读 · 0 评论 -
[LintCode] 落单的数 III Single Number III
给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字。 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度,O(1)的额外空间复杂度Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example Given [1,2,2,3原创 2016-04-07 13:49:58 · 613 阅读 · 0 评论 -
[LintCode] 字符大小写排序 Sort Letters by Case
给定一个只包含字母的字符串,按照先小写字母后大写字母的顺序进行排序。 注意事项 小写字母或者大写字母他们之间不一定要保持在原始字符串中的相对位置。 样例 给出”abAcD”,一个可能的答案为”acbAD” 挑战 在原地扫描一遍完成Given a string which contains only letters. Sort it by lower case first and uppe原创 2016-04-08 15:29:13 · 852 阅读 · 0 评论 -
[LintCode] 螺旋矩阵 Spiral Matrix
给定一个包含 m x n 个要素的矩阵,(m 行, n 列),按照螺旋顺序,返回该矩阵中的所有要素。样例 给定如下矩阵: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 应返回 [1,2,3,6,9,8,7,4,5]。Given a matrix of m x n elements (m rows, n columns), return all原创 2016-04-25 21:37:11 · 2712 阅读 · 0 评论 -
[LintCode] 格雷编码 Gray Code
格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个二进制的差异。 给定一个非负整数 n ,表示该代码中所有二进制的总数,请找出其格雷编码顺序。一个格雷编码顺序必须以 0 开始,并覆盖所有的 2n 个整数。注意事项 对于给定的 n,其格雷编码顺序并不唯一。 根据以上定义, [0,2,3,1] 也是一个有效的格雷编码顺序。样例 给定 n = 2, 返回 [0,1,3,2]。其格雷编原创 2016-04-14 21:30:05 · 2818 阅读 · 0 评论 -
[LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal
根据前序遍历和中序遍历树构造二叉树. 注意事项 你可以假设树中不存在相同数值的节点.样例 给出中序遍历:[1,2,3]和前序遍历:[2,1,3]. 返回如下的树: 2 / \ 1 3Given preorder and inorder traversal of a tree, construct the binary tree. Notice You may assume原创 2016-04-17 22:27:18 · 578 阅读 · 0 评论 -
[LintCode] 中序遍历和后序遍历树构造二叉树 Construct Binary Tree from Inorder and Postorder Traversal
根据中序遍历和后序遍历树构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 / \ 1 3Given inorder and postorder traversal of a tree, construct the binary tree. Notice You may原创 2016-04-18 20:25:03 · 701 阅读 · 0 评论 -
[LintCode] 用递归打印数字 Print Numbers by Recursion
用递归的方法找到从1到最大的N位整数。你能够用深度最多只有 N 层的递归么?样例 给出 N = 1, 返回[1,2,3,4,5,6,7,8,9]. 给出 N = 2, 返回[1,2,3,4,5,6,7,8,9,10,11,…,99].Print numbers from 1 to the largest number with N digits by recursion. Can you re原创 2016-04-19 16:14:22 · 980 阅读 · 0 评论 -
[LintCode] 最长公共子序列 Longest Common Subsequence
给出两个字符串,找到最长公共子序列(LCS),返回LCS的长度。说明 最长公共子序列的定义: 最长公共子序列问题是在一组序列(通常2个)中找到最长公共子序列(注意:不同于子串,LCS不需要是连续的子串)。该问题是典型的计算机科学问题,是文件差异比较程序的基础,在生物信息学中也有所应用。 https://en.wikipedia.org/wiki/Longest_common_subsequen原创 2016-05-04 10:59:50 · 572 阅读 · 0 评论 -
[LintCode] 快速幂 Fast Power
计算a^n % b,其中a,b和n都是32位的整数。样例 例如 231 % 3 = 2 例如 1001000 % 1000 = 0挑战 O(logn)Calculate the a^n % b where a, b and n are all 32bit integers.Example For 231 % 3 = 2 For 1001000 % 1000 = 0Challenge原创 2016-04-20 20:17:53 · 1632 阅读 · 0 评论 -
[LintCode] 二叉树的层序遍历 Binary Tree Level Order Tranversal
给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问)样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3], [9,20], [15,7] ]只使用一个队列:/** * Definition of TreeNode: * public class Tree原创 2016-04-20 21:06:18 · 674 阅读 · 0 评论 -
[LintCode] 字符串置换 String Permutation
给定两个字符串,请设计一个方法来判定其中一个字符串是否为另一个字符串的置换。 置换的意思是,通过改变顺序可以使得两个字符串相等。样例 “abc” 为 “cba” 的置换。 “aabc” 不是 “abcc” 的置换。Given two strings, write a method to decide if one is a permutation of the other. Example原创 2016-04-21 21:03:42 · 3375 阅读 · 3 评论 -
[LintCode] 二叉树的路径之和 Binary Tree Path Sum
给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。 一个有效的路径,指的是从根节点到叶节点的路径。样例 给定一个二叉树,和 目标值 = 5: 1 / \ 2 4 / \ 2 3 返回: [ [1, 2, 2], [1, 4] ]Given a binary tree, find all paths that s原创 2016-04-22 09:38:38 · 3909 阅读 · 1 评论 -
[LintCode] 带最小值操作的栈 Min Stack
实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成。样例 如下操作:push(1),pop(),push(2),push(3),min(), push(1),min() 返回 1,2,1Implement a stack with min() function, which will re原创 2016-04-22 15:48:51 · 1749 阅读 · 0 评论 -
[LintCode] 旋转图像 Rotate Image
给定一个N×N的二维矩阵表示图像,90度顺时针旋转图像。 样例 给出一个矩形[[1,2],[3,4]],90度顺时针旋转后,返回[[3,1],[4,2]] 挑战 能否在原地完成?You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Exampl原创 2016-04-11 15:43:36 · 1069 阅读 · 0 评论 -
[LintCode] 跳跃游戏II Jump Game II
给出一个非负整数数组,你最初定位在数组的第一个位置。 数组中的每个元素代表你在那个位置可以跳跃的最大长度。 你的目标是使用最少的跳跃次数到达数组的最后一个位置。样例 给出数组A = [2,3,1,1,4],最少到达数组最后一个位置的跳跃次数是2(从数组下标0跳一步到数组下标1,然后跳3步到数组的最后一个位置,一共跳跃2次)Given an array of non-negative in原创 2016-04-11 15:13:07 · 893 阅读 · 0 评论 -
[LintCode] 将二叉查找树转换成双链表 Convert Binary Search Tree to Doubly Linked List
将一个二叉查找树按照中序遍历转换成双向链表。 样例 给定一个二叉查找树: 4 / \ 2 5 / \ 1 3 返回 1<->2<->3<->4<->5。Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary se原创 2016-04-25 22:26:32 · 2423 阅读 · 0 评论 -
[LintCode] 复制带随机指针的链表 Copy List with Random Pointer
给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点。 返回一个深拷贝的链表。 挑战 可否使用O(1)的空间A linked list is given such that each node contains an additional random pointer which could point to any node in the list or nul原创 2016-04-25 23:19:58 · 1160 阅读 · 0 评论