随笔分类 -  算法

【题解】【BST】【Leetcode】Convert Sorted Array to Binary Search Tree
摘要: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 ... 阅读全文

posted @ 2014-02-02 19:27 小唯THU 阅读(782) 评论(0) 推荐(0)

【题解】【链表】【Leetcode】Add Two Numbers
摘要: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思路:乍一看以为是大整数,实则相当简单,比 阅读全文

posted @ 2014-02-02 15:56 小唯THU 阅读(235) 评论(0) 推荐(0)

【题解】【字符串】【Leetcode】Valid Palindrome
摘要: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 阅读全文

posted @ 2014-02-01 18:00 小唯THU 阅读(207) 评论(0) 推荐(0)

【题解】【DP】【Leetcode】Climbing Stairs
摘要: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); ... 阅读全文

posted @ 2014-02-01 13:53 小唯THU 阅读(474) 评论(0) 推荐(0)

【题解】【数组】【Leetcode】Merge Sorted Array
摘要: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 阅读全文

posted @ 2014-02-01 13:06 小唯THU 阅读(206) 评论(0) 推荐(0)

【题解】【直方图】【Leetcode】Trapping Rain Water
摘要: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 阅读全文

posted @ 2014-01-26 12:27 小唯THU 阅读(263) 评论(0) 推荐(0)

【题解】【大整数】【Glassdoor】
摘要: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 阅读全文

posted @ 2014-01-15 16:33 小唯THU

经典算法题一览
摘要:0. 零基础入门Coding interview exposed (3ed) 这个最简单了,基础比较挫的可以从这里开始“这本书籍不是“课本”,不是“课程”,而是教你做各种常见面试题目的。熟练掌握这本书籍的内容,是你找到工作的基础。”Programming Interviews Exposed 8.0... 阅读全文

posted @ 2013-09-13 12:52 小唯THU 阅读(10819) 评论(1) 推荐(5)

Directed Minimum Spanning Tree: Chu-Liu/Edmonds Algorithm
摘要:我们的现代数据库大作业要求实现一个图查询系统,包括基于属性的子图查询、可达性查询(可选)、最短路径查询(可选)、TopK最短路径查询(可选)、图形化展示(可选)等功能。分成子图同构查询小组以及可达性及TopK路径查询小组。 小组长之前研究了Efficiently answering reachability queries on very large directed graphs这篇论文,关于P... 阅读全文

posted @ 2013-01-02 14:25 小唯THU 阅读(3709) 评论(2) 推荐(1)

KMP算法C语言实现
摘要:#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, 阅读全文

posted @ 2012-12-17 08:41 小唯THU 阅读(499) 评论(0) 推荐(0)

快排专题
摘要:快速排序详细分析精通八大排序算法系列:一、快速排序算法 十二之续、快速排序算法的深入分析 十二之再续:快速排序算法之所有版本的c/c++实现 《大话数据结构》第9章 排序 9.9 快速排序(下)四种快速排序各种内部排序方法的比较和选择 阅读全文

posted @ 2012-10-07 02:06 小唯THU 阅读(286) 评论(0) 推荐(0)

C++随机数--——生成任意范围内等概率随机数“足够好”的做法
摘要:很容易想到一个简单的做法 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不均匀了。举个例... 阅读全文

posted @ 2012-10-04 18:52 小唯THU 阅读(3200) 评论(0) 推荐(1)

导航