- 博客(15)
- 资源 (7)
- 收藏
- 关注
原创 寻找第k小的元素或者第k大的元素 -- O(n)
此题第一思路肯定是, #include #include using namespace std; int partition(int *A,int s,int e);//划分 int select_k(int * A,int n,int k); int select_k_recursive(int * A,int s,int e,int k);//选出第k个大的数 int m
2014-05-29 09:58:10 1359
原创 归并排序
sho #include #include #include "vld.h" using namespace std; void merge_sort_recursive(int *A,int low ,int high); void merge_array(int *A,int low ,int mid ,int high); void merge_sort(int *A,int n)
2014-05-26 21:42:31 469
原创 快速排序--个人思路与书本思路 (复习排序算法)
快速排序的平均时间 #include #include #include using namespace std; int partition(int *A,int low,int high); void qsort(int *A,int n); int q_partition(int *A,int low,int high); void q_sort(int *A,int n)
2014-05-26 17:38:52 523
原创 打印n对括号匹配的所有组合
像er #include #include #include #include using namespace std; void paren(int n,int level,int count_left,int count_right,vector &res,string str); void paren_str(int n); int main() { paren_str(4
2014-05-20 11:14:51 1244
原创 组合 问题
有俩种思路: 1, #include #include #include "vld.h" using namespace std; void combine_str(string str); void combine(string str,int level,string &res,bool *used); bool is_repeat(string str ,int k,bool *us
2014-05-19 21:48:09 503
原创 一个数组arr,长度为N,N小于10^8,里面数字的范围是1~N,是否有重复,并统计出来
#include #include #include #include using namespace std; void count_num(int *array,int n); int main() { int array[10]={4,3,2,3,4,5,2,8,6,2}; count_num(array,10); for (int i = 0 ;i < 10;i++)
2014-05-16 10:52:08 1432
原创 括号匹配问题
用了俩种方式实现,递归,非递归。 此题 #include #include #include using namespace std; bool match(char *str,int &index,int &num); bool is_valid(char *str); bool simple_isvalid(char *str); int main() { char s
2014-05-15 22:18:33 527
原创 丑数
我们把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第1500个丑数。 注:1是第一个丑数。 简单的zuofashi
2014-05-13 19:58:40 535
原创 编程之美-先序中序 重建二叉树
#include #include #include "vld.h" using namespace std; struct Node { Node *left; Node *right; char val; Node(char ch) { left = NULL; right = NULL; val = ch; } }; void pre_order(Node
2014-05-03 22:59:36 549
原创 先序中序转后序
#include #include using namespace std; string pre_in_to_post(string str_pre,string str_inorder,string left,string right,string &result); int main() { string str_pre,str_in,str_post; str_pre = "
2014-05-03 20:07:12 743
转载 编程之美3.3——计算字符串的相似度
问题: 1. 计算两个字符串的最长公共子序列(LCS),且公共子序列在字符串中不需要是连续的。 2. 计算两个字符串的距离,完全相同的字符串距离为0,可以通过修改一个字符、增加一个字符或删除一个字符三种方式来使两个字符串相同,但这些方式会使得距离加1。 1.解法: 这两个问题的解法基本相同,都是二维的动态规划,都考虑字符串的后缀(实际上用动态规划更喜欢考虑前缀,但使用前缀时数组最好
2014-05-03 11:11:10 483
原创 堆排序,以及stl中的堆应用
#include #include #include #include using namespace std; void recursive_max_heap_shift_down(int *array,int start,int end); void make_max_heap(int *array,int start,int end); void nonrecursive_make
2014-05-02 16:31:55 724
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人