-
LeetCode OJ——Unique Binary Search Trees II
摘要:http://oj.leetcode.com/problems/unique-binary-search-trees-ii/一题要求得出所有树的种类数,二题要求得出所有树。在一题的基础上修改代码,还是要提前想清楚再写。#include #include using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} };cl...
阅读全文
-
回调函数 typedef bool (*IsUsed)(const string &name,boost::shared_ptr<ShpGeometry> oneGeometry);
摘要:就是指向函数的指针。回调函数,表示了一个函数的地址,将函数作为参数进行使用。参考百度百科:http://baike.baidu.com/view/414773.htm常用的大概就是在sort函数中了吧。回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。例子如下:函数:typedefint(__stdcall *CompareFunction)(constbyte*,constbyt
阅读全文
-
LeetCode OJ——Convert Sorted List to Binary Search Tree
摘要:http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/将一个按照元素升序排列的链表转换成BST。根据自身性质“元素升序排列”,再参考将升序排列的数组转换成BST的上一道题目,只需要将对数组的处理方式变成对链表的。还是得好好利用元素已经升序排列这个性质,不用那种纯粹的重新生成一个BST,按照左转、右转、先左转后右转、先右转后左转。#include #include using namespace std;struct ListNode { int val; ListNode *next;...
阅读全文
-
搜索——二叉搜索树
摘要://二t叉?搜?索??树???的??搜?索??#include #include typedef int KeyType;typedef struct node{ KeyType key; struct node *lchild,*rchild;}BSTNode;int Compare(int index1,int index2){ if(index1>index2) return 1; if(index1...
阅读全文
-
递归——汉诺塔、背包
摘要:#include using namespace std;void move_disk(char src,char dst){ cout">disks; towers(disks, 'A','B' ,'C'); return 0;}//物?品??重?量??S,??共2n件t物?品??,??从???其?中D选?出?若??干??件t放??在??背?3包???里??,??使?1得??重?量??之?和??为as#includeusing namespace std;int W[10];bool Knap(int...
阅读全文
-
数据结构自己实现——Linklist
摘要://单???链???表???#include using namespace std;typedef char datatype;typedef struct node{ datatype data; struct node* next;}listnode;typedef listnode* linklist;listnode *p;//建??立???链???表???linklist createlist(){ linklist head = (listnode*)malloc( sizeof(list...
阅读全文
-
数据结构自己实现——Tree and Forest
摘要://中D序??遍???历???二t叉?树???//先??序??遍???历???二t叉?树???//后??序??遍???历???二t叉?树???#include using namespace std;typedef char DataType;struct BiNode{ DataType data; struct BiNode *lchild,*rchild;};void inOrder(BiNode *p){ if(p!=NULL) { ...
阅读全文
-
数据结构自己实现——queue
摘要:SeqQueue.h#define QueueSize 100typedef char DataType;class SeqQueue{public: DataType data[QueueSize]; int front; int rear; void Initial(); bool IsEmpty(); bool IsFull(); void EnQueue(DataTy...
阅读全文
-
数据结构自己实现——stack
摘要:#define StackSize 100typedef char DataType;class stack{public: DataType data[StackSize]; int top; void Initial(); bool IsEmpty(); bool IsFull(); void Push(DataType x); DataTy...
阅读全文
-
LeetCode OJ——Convert Sorted Array to Binary Search Tree
摘要:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/将一个升序的数组转换成height balanced BST高度平衡的二叉搜索树,根据二叉搜索树的特征,所有比根节点小的值,都在根节点的左边,所有比根节点大的值,都在根节点的右边。建立的过程就是一个个的插入。但要求是高度平衡的,即不能是各种偏的那样,否则的话,搜索的代价会增大,最佳的时候是O(height),height balanced的时候也是O(height).所以会涉及到各种左旋转,右旋转,先左旋再右旋,先右旋再左旋的操作(为了平衡高度)
阅读全文
-
LeetCode OJ——Plus One
摘要:http://oj.leetcode.com/problems/plus-one/进位加法#include #include using namespace std;class Solution {public: vector plusOne(vector &digits) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vector ansV...
阅读全文
-
LeetCode OJ——Pascal's Triangle II
摘要:http://oj.leetcode.com/problems/pascals-triangle-ii/杨辉三角2,比杨辉三角要求的空间上限制Could you optimize your algorithm to use onlyO(k) extra space?其实计算当前行,也只用到前一行了。再前面的没有用。class Solution {public: vector getRow(int rowIndex) { // IMPORTANT: Please reset any member data you declared, as // the same...
阅读全文
-
LeetCode OJ——Pascal's Triangle
摘要:http://oj.leetcode.com/problems/pascals-triangle/杨辉三角先分析数据,找出规律ans[row][col] = ans[row-1][col-1]+ans[row-1][col]#include #include #include using namespace std;class Solution {public: vector > generate(int numRows) { // IMPORTANT: Please reset any member data you declared, as // the ...
阅读全文
-
LeetCode OJ——Minimum Depth of Binary Tree
摘要:http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/贡献了一次runtime error,因为如果输入为{}即空的时候,出现了crash。其实这种情况也处理了,但是顺序不对,如下:if(root->left == NULL && root->right == NULL) return 1;if(root==NULL )return 0;如果输入是空的话,会对空->left访问left,但这是错误的。所以,应该先判断是否为空。即把两个if换个顺序就好了。广搜,一直到有个点是叶子节点,然后返回深
阅读全文
-
LeetCode OJ——Word Ladder2
摘要:http://oj.leetcode.com/problems/word-ladder-ii/class Solution {public: vector> findLadders(string start, string end, unordered_set &dict) { //map parentRecord; multimap parentRecord; queue > wordQueue; unordered_set visited; wordQueue.push(make_pair(start,1)...
阅读全文
-
LeetCode OJ——Climbing Stairs
摘要:http://oj.leetcode.com/problems/climbing-stairs/走台阶的题目,转换出数学模型之后,就是Fibonacci数列。后一个数等于前两个数的和。递归版本超时,代码如下:class Solution {public: int walk(int sum) { if(sum == 0 ) return 1; if(sum ==1) return 1; return walk(sum-1)+walk(sum-2); } int climbStairs(i...
阅读全文
-
C++ 细节知识
摘要:1.typedef struct child{string name;struct child* next;};child* head;head = (child*)malloc(sizeof(child));cin>>head->name;0x00c42386处有未经处理的异常:oxc0000005:写入位置oxcdcdcd时发生访问冲突。0xC0000005表示使用了未创建(未分配内存空间)的变量或对象。0xC0000005错误通常是由于内存访问错误引起,可能是你的程序有错误的指针操作或者访问了空对象,内存未初始化。child* head;未初始化变量啊string na
阅读全文
-
设计模式——观察者模式
摘要:材料参考百度百科http://baike.baidu.com/view/1854779.htm#5_2 观察者模式(有时又被称为发布-订阅Subscribe>模式、模型-视图View>模式、源-收听者Listener>模式或从属者模式)是软件设计模式的一种。在此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。这通常透过呼叫各观察者所提供的方法来实现。此种模式通常被用来实作事件处理系统。观察者模式有很多实现方式,从根本上说,该模式必须包含两个角色:观察者和被观察对象。观察者和被观察者之间存在“观察”的逻辑关联,当被观察者发生改变的时候,
阅读全文
-
LeetCode OJ——Word Ladder
摘要:http://oj.leetcode.com/problems/word-ladder/图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜。深搜版本:第一次写的时候,把sum和visited都自然的设置成了传引用,导致递归调用下去之后,再返回来,反而各种参数的值退不回来了。然后把sum和visited改成了传值,这样反而适应了本程序意图。可见,也不是什么时候都需要传引用的。具体在写程序的时候,需要传值还是传引用,要具体分析。传引用和传值的情况分别如下:void DFS(string currentWord,string endWord,int &sum, unordered_
阅读全文
-
unorder_set<typename T> 学习
摘要:转自http://blog.csdn.net/mmzsyx/article/details/8240071散列容器(hash container):通常比二叉树的存储方式可以提供更高的访问效率.#include #include using namespace boost;散列集合简介:unordered库提供两个散列集合类unordered_set和unordered_multiset,STLport也提供hash_set和hash_multiset,它们的接口,用法与stl里的标准关联容器set/multiset相同,只是内部使用散列表代替了二叉树实现,因此查找复杂度由数降为常数。unor
阅读全文
-
LeetCode OJ——Text Justification
摘要:http://oj.leetcode.com/problems/text-justification/编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int 2 #include 3 #include 4 using namespace std; 5 6 class Solution { 7 public: 8 vector fullJustify(vector & words, int L) { 9 // IMPORTANT: Please reset any member data you decla...
阅读全文
|