![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Algo. and Data structure
文章平均质量分 73
L_J_SHOU
从蛛丝马迹中寻找结果
展开
-
T(n) = T(n/2) + O(n)
先通过一个简单的例子,T(n) = T(n/2) + O(n), 那么T(n)应该的等于多少呢? 1. 给个直观的例子: 自从看到 An apple a day, keep doctors away, 这句谚语,每天我都会吃苹果。 但我吃苹果的方式很特殊,先吃半个,再吃剩下的半个的一半,再吃剩下的半个的一半。。。。 结论: 我永远都吃不完这一个苹果。 其实这个例子就给出了T(n)的上界是原创 2015-02-14 23:08:00 · 6686 阅读 · 1 评论 -
Letter combinations of a phone number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string原创 2014-04-02 16:05:20 · 930 阅读 · 0 评论 -
Sqrt(x)
解法1: 牛顿切线法解法2: 二分法原创 2014-02-12 21:59:09 · 1036 阅读 · 0 评论 -
Copy List with Random Pointer 深度拷贝,浅度拷贝,Lazy拷贝解析
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.三种拷贝的分析: 深度拷贝,浅度拷贝,Lazy拷贝原创 2014-01-27 16:50:21 · 3575 阅读 · 0 评论 -
3 Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c原创 2014-02-21 22:43:44 · 1307 阅读 · 2 评论 -
二叉树中,找出和为某值的所有路径
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2014-02-24 20:48:51 · 1231 阅读 · 0 评论 -
Word Break 2
Given a string s and a dictionary of wordsdict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens = "cat原创 2014-03-04 16:59:21 · 1155 阅读 · 0 评论 -
Unique Binary Search Trees 2
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3原创 2014-03-31 21:10:00 · 851 阅读 · 0 评论 -
Longest Consecutive Sequence
Description:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence原创 2014-02-15 20:10:14 · 921 阅读 · 0 评论 -
Insertion sort list
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* in原创 2014-02-16 17:11:40 · 918 阅读 · 0 评论 -
Median of Two Sorted Arrays
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).原创 2014-02-16 15:00:27 · 1002 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating let原创 2014-02-16 13:14:58 · 1080 阅读 · 0 评论 -
Max Points On a Line
Max Points on a LineTotal Accepted: 4246 Total Submissions: 42602 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.分析: 任取两点组成的直线,看有多少点在其上面原创 2014-02-13 10:04:39 · 1446 阅读 · 0 评论 -
数据结构《20》----Immutable stack
有趣的函数式数据结构《一》----不可变栈什么是不可变?往栈中插入一个元素,原来的栈保持不变,返回一个新的栈(已插入新的元素)。push, pop,getMax 等操作都要求在 常数时间内完成。可能读者会产生疑惑,既然要返回一个新的栈,是不是就必须先拷贝一份原来的栈,然后在新的栈中插入元素。但是这样复杂度就是线性的,如何能够在常数时间内完成呢??这里,就是immutab原创 2014-06-17 14:55:42 · 1826 阅读 · 0 评论 -
算法导论----VLSI芯片测试; n个手机中过半是好的,找出哪些是好手机
对于分治(Divide and Conquer)的题目,最重要是1.如何将原问题分解为若干个子问题,2.子问题中是所有的都需要求解,还是选择一部分子问题即可。还有一点其实非常关键,但是往往会被忽视:分解后的子问题除了规模较原问题小之外,必须和原问题具有相同的性质。在子问题的划分时,只有这一点保证,才能递归求解子问题,从而得到solution。下面通过几个例子,详原创 2013-11-04 17:55:42 · 3325 阅读 · 15 评论 -
算法----bonus dumplings
题目描述过年了,妈妈做了100只饺子,其中有10只饺子里面有1块的硬币。小明依次吃这100只饺子,如果小明连续吃到k个硬币,那么小明得到k-1个硬币。e.g. 110111表示6只饺子,1表示有硬币,0表示没有。11表示连续吃到2个饺子,那么小明得1个硬币;111连续迟到3个,小明得2个硬币;故,小明共得到3个硬币。问小明得到的硬币的期望值是多少?分析原创 2014-12-28 20:02:22 · 1251 阅读 · 0 评论 -
博客新站点 ljshou.gitcafe.io
在 Github 和 Gitcafe 上用 octopress 搭了博客。以后的博客都会放在 ljshou.gitcafe.com原创 2014-10-21 09:56:03 · 1222 阅读 · 2 评论 -
计算几何----判断空间点是否在一个四面体(tetrahedron)内部
DESCRIPTION:判断空间点 P(x, y, z)是否在一个四面体的内部?Let the tetrahedron have vertices V1 = (x1, y1, z1) V2 = (x2, y2, z2) V3 = (x3, y3, z3) V4 = (x4, y4, z4)and your test原创 2013-12-20 15:31:35 · 5223 阅读 · 0 评论 -
算法----约瑟夫环问题
约瑟夫环是一个数学的应用问题:已知n个人(以编号0,1,2, ... n-1 分别表示)围坐在一张圆桌周围。从编号为0的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。求最后一个出列的人的序号。。方法 1:模拟游戏。利用数组或者循环链表,模拟游戏的进行,直到最后仅仅留下一个数。复杂度是 O(MN)原创 2014-03-11 22:06:21 · 1620 阅读 · 7 评论 -
背包问题九讲 [转]
P01: 01背包问题题目有N件物品和一个容量为V的背包。第i件物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。基本思路这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。用子问题定义状态:即f[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。则其状态转移方程便是:f[i][v]=max{f[i-1][v],f转载 2013-11-22 21:00:15 · 1130 阅读 · 1 评论 -
函数递归调用: 调用者 与 被调用者之间的四种数据传递关系
递归就是函数调用本身,来解决子问题,通过子问题的解构造本问题的解。递归可以用于简化程序,让程序结构明了。。调用者与被调用者之间有以下的四种数据传递关系:1>没有任何数据传递 e.g. 打印链表void PrintList(ListNode const *list){ if(list == NULL) return; //exit cout val <<原创 2013-12-13 11:33:01 · 2298 阅读 · 0 评论 -
编程之美----数组的最长递增子序列
DESCRIPTION:数组的最长递增子序列(LIS)Given a sequence of n real numbers a1, ..., an, determinea subsequence (notnecessarily contiguous) of maximum length in which thevalues in the sub-sequence form a st原创 2013-10-31 17:07:59 · 5378 阅读 · 2 评论 -
算法----中位数算法的妙用(更新中)
部分背包问题:一个窃贼去一家商店偷窃,有n件商品: 第i件物品值Vi元,重wi榜(vi, wi都是整数),他的背包最多只能装下W榜物品,每件商品他可以选择一部分带走,而不是像0-1背包问题。问他最多能带走多贵的物品?分析: 由于部分背包问题允许仅拿走物品的一部分,物件更像是金粉,可证明其具有贪心的性质。算法1: 贪心按照每榜的价值进行排序,然后由价值的大小依次往包里装,原创 2013-11-07 20:40:54 · 7313 阅读 · 2 评论 -
算法题----称硬币: 2n(并不要求n是2的幂次方)个硬币,有两个硬币重量为m+1, m-1, 其余都是m 分治 O(lgn)找出假币
Description:有2n个硬币和一个天平,其中有一个质量是m+1, 另一个硬币质量为m-1, 其余的硬币质量都是m算法1:O(n)算法将2n个硬币分成n组(每组2个)进行称量:结果只有两种: 1. 仅有一组出现天平不平衡: 一定就是 两个假币 2. 出现两组天平不平衡: 这四个硬币中必定存在两个假币。将重的硬原创 2013-12-07 16:00:34 · 4412 阅读 · 9 评论 -
算法----序列和的 top N
Description:两个长度为 n 的数组 A 和 B, 各从中选出一个元素相加 A[i] + B[j], 求 top n 小的那些和。http://www.cnblogs.com/372465774y/archive/2012/07/09/2583866.html原创 2014-02-14 13:08:02 · 2884 阅读 · 0 评论 -
数学----有趣的扑克牌《一》
问题:一副扑克牌,除去大小王后共52张牌,随机从中抽八张牌,问八张牌的和最有可能是多少?分析:这52张牌,其实就是数字 1 2 3 。。。13, 每个数字出现4次。随机抽出8个数,问组成的和最有可能是多少?聪明的你可能想到了另一个很类似的问题,2 sum: 问一个数组中是否存在两个数的和等于某个给定的值。当然,这里就类似于 8 sum。 但是,题目却问的是,最有原创 2014-05-08 22:37:17 · 1373 阅读 · 0 评论 -
分治算法复杂度计算----算法导论 主定理应用讲解
本文讨论如何有效应用复杂度计算的主定理快速计算 分治算法的复杂度。下面给出几个简单的例子:1. T(n) = T(n/2) + O(n)f(n) = O(n) > O(1), 并且满足第四种情况,所以复杂度是O(n)2. T(n) = 2T(n/2) + O(n)f(n) = O(n) = O(n), so complexity is O(nlgn), sati原创 2013-12-24 16:52:59 · 3351 阅读 · 0 评论 -
Sort List
Sort a linked list in O(n log n) time using constant space complexity.采用 归并排序。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN原创 2014-02-16 17:10:06 · 1188 阅读 · 0 评论 -
First Missing Positive
Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant spa原创 2014-02-12 21:33:32 · 939 阅读 · 0 评论 -
Word Ladder 2
图的BFS的一个应用---Leetcode: word ladder2原创 2014-01-24 11:10:07 · 1473 阅读 · 0 评论 -
数据结构《15》----二叉树的层序遍历
输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。例如输入 8 / \ 6 10 / \ / \ 5 7 9 11输出 8 6 10 5 7 9 11。分析:与二叉树其他的三种遍历不同,层序遍历使用的不是栈,而是队列。// copyright @ L.J.S原创 2014-03-10 19:37:18 · 1062 阅读 · 0 评论 -
数据结构《14》----并查集 Union-Find
描述: 并查集是一种描述解决等价关系。能够方便地描述不相交的多个集合。 支持如下操作 1. 建立包含元素 x 的集合 MakeSet(x) 2. 查找给定元素所在的集合 Find(x), 返回所在集合的代表 3. 将两个不相交的集合合并 Union原创 2014-02-26 19:51:24 · 2536 阅读 · 2 评论 -
数据结构《9》----Threaded Binary Tree 线索二叉树
Definition:"A binary tree is threaded by making all right child pointers that would normally be null point to the inorder successor of the node, and all left child pointers that would normally be原创 2014-02-10 22:30:01 · 3498 阅读 · 5 评论 -
数据结构 《8》----二叉树的构建,从前序(后序)和中序
从二叉树的 中序遍历 和 前(后)序遍历可以唯一地确定一棵二叉树。原创 2014-01-30 15:35:12 · 1463 阅读 · 0 评论 -
数据结构 《6》----堆 ( Heap )
经典数据结构----堆的具体实现,以及应用堆解决的4个实际问题原创 2014-01-28 22:01:16 · 1352 阅读 · 1 评论 -
数据结构 《5》----二叉搜索树 ( Binary Search Tree )
本文分析了二叉搜索树的各种操作的具体实现。原创 2014-01-18 16:23:22 · 1727 阅读 · 0 评论 -
数据结构 《4》---- 一个漂亮的打印二叉树的程序
一个漂亮地打印二叉树的一个小程序。原创 2014-01-17 20:50:16 · 5881 阅读 · 3 评论 -
数据结构 《3》----双向链表(带有头节点)
本文参照了 C与指针中双向链表的实现,力求简单明了。双向链表带有一个头节点, 头节点的pre指针指向链表的最后一个node; next指向第一个node; val 表示链表的长度。// copyright @ L.J.SHOU Jan.15, 2014// double-linked-list with a head node#i原创 2014-01-15 21:48:17 · 4031 阅读 · 1 评论 -
数据结构 《2》----基于邻接表表示的图的实现 DFS(递归和非递归), BFS
本文给出了图的基于邻接表表示的实现。实现了DFS, BFS等图的基本操作。原创 2014-01-13 15:32:41 · 1961 阅读 · 0 评论 -
数据结构《1》---单链表
单链表各种操作的实现原创 2014-01-12 18:31:29 · 1751 阅读 · 0 评论