摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:对于树来说,自顶向下的递归是很好控制的,自底向上的递归就很容易让脑神经打结了。本来想仿照排序二叉树的中序遍历,逆向地由数组构造树,后来发现这样做需要先确定树的结构,不然会一直递归下去,不知道左树何时停止。代码:TreeNode *addNode(vector &num, int start, int end){ if(start > end) return NULL; int ...
阅读全文
摘要:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)Output:7 -> 0 -> 8思路:乍一看以为是大整数,实则相当简单,比
阅读全文
摘要:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama"is a palindrome."race a car"isnota palindrome.Note:Have you consider that the string might be empty? This is a good question to
阅读全文
摘要:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?思路:最简单的DP问题:递归+查表代码: 1 int climbStairs(int n) { 2 vector memo(n+1, -1);//忘了在memo[0]没有意义的时候,数组初始大小加一。。。 3 return climb(n, memo); ...
阅读全文
摘要:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.思路:归并的一部分,比Merge Two Sorted Lists还要简单代码: 1 void merge(int A[], int m, in
阅读全文
摘要:Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of
阅读全文
摘要:Two algorithm problems, one is given 4 digits array, like 0,0,0,0, plus 1 into this array and print. Ex. 8,8,8,8 to 8,8,8,9.consider one situation, 9,9,9,9 to 1,0,0,0,0.print all numbers between 1 and a million
阅读全文
摘要:0. 零基础入门Coding interview exposed (3ed) 这个最简单了,基础比较挫的可以从这里开始“这本书籍不是“课本”,不是“课程”,而是教你做各种常见面试题目的。熟练掌握这本书籍的内容,是你找到工作的基础。”Programming Interviews Exposed 8.0...
阅读全文
摘要:我们的现代数据库大作业要求实现一个图查询系统,包括基于属性的子图查询、可达性查询(可选)、最短路径查询(可选)、TopK最短路径查询(可选)、图形化展示(可选)等功能。分成子图同构查询小组以及可达性及TopK路径查询小组。
小组长之前研究了Efficiently answering reachability queries on very large directed graphs这篇论文,关于P...
阅读全文
摘要:#include "stdafx.h" #include void compute_prefix(char P[], int length, int pi[]); void kmp_matcher(char T[],char P[],int pi[], int length_t, int length_p); int _tmain(int argc, _TCHAR* argv[]) { char T[]="bacbabababcbab"; char P[]="ababababca"; int pi[7]; kmp_matcher(T,
阅读全文
摘要:快速排序详细分析精通八大排序算法系列:一、快速排序算法 十二之续、快速排序算法的深入分析 十二之再续:快速排序算法之所有版本的c/c++实现 《大话数据结构》第9章 排序 9.9 快速排序(下)四种快速排序各种内部排序方法的比较和选择
阅读全文
摘要:很容易想到一个简单的做法
x = rand () % RANGE; /* Poor! return a random number in [0,RANGE) */ 这种做法有三个问题:
1 rand()等概率返回[0, RAND_MAX]间这RAND_MAX+1个数中的任意一个,如果RAND_MAX+1不能被RANGE整除,那么这个[0, RANGE)分布就blatantly不均匀了。举个例...
阅读全文