摘要:
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换个顺序就好了。广搜,一直到有个点是叶子节点,然后返回深
阅读全文
posted @ 2013-11-14 16:14
qingcheng奕
阅读(219)
推荐(0)
摘要:
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)...
阅读全文
posted @ 2013-11-13 16:14
qingcheng奕
阅读(272)
推荐(0)
摘要:
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...
阅读全文
posted @ 2013-11-10 22:12
qingcheng奕
阅读(194)
推荐(0)
摘要:
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
阅读全文
posted @ 2013-11-09 20:27
qingcheng奕
阅读(452)
推荐(0)
摘要:
材料参考百度百科http://baike.baidu.com/view/1854779.htm#5_2 观察者模式(有时又被称为发布-订阅Subscribe>模式、模型-视图View>模式、源-收听者Listener>模式或从属者模式)是软件设计模式的一种。在此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。这通常透过呼叫各观察者所提供的方法来实现。此种模式通常被用来实作事件处理系统。观察者模式有很多实现方式,从根本上说,该模式必须包含两个角色:观察者和被观察对象。观察者和被观察者之间存在“观察”的逻辑关联,当被观察者发生改变的时候,
阅读全文
posted @ 2013-11-09 16:27
qingcheng奕
阅读(285)
推荐(0)
摘要:
http://oj.leetcode.com/problems/word-ladder/图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜。深搜版本:第一次写的时候,把sum和visited都自然的设置成了传引用,导致递归调用下去之后,再返回来,反而各种参数的值退不回来了。然后把sum和visited改成了传值,这样反而适应了本程序意图。可见,也不是什么时候都需要传引用的。具体在写程序的时候,需要传值还是传引用,要具体分析。传引用和传值的情况分别如下:void DFS(string currentWord,string endWord,int &sum, unordered_
阅读全文
posted @ 2013-11-02 21:09
qingcheng奕
阅读(278)
推荐(0)
摘要:
转自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
阅读全文
posted @ 2013-11-01 20:23
qingcheng奕
阅读(772)
推荐(0)
摘要:
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...
阅读全文
posted @ 2013-11-01 10:30
qingcheng奕
阅读(215)
推荐(0)
摘要:
http://oj.leetcode.com/problems/two-sum/求是否存在两个数的和为target,暴力法,两层循环#include #include #include using namespace std;class Solution {public: vector twoSum(vector &numbers, int target) { // Note: The Solution object is instantiated only once and is reused by each test case. //sort(number...
阅读全文
posted @ 2013-10-26 11:46
qingcheng奕
阅读(184)
推荐(0)
摘要:
http://oj.leetcode.com/problems/longest-valid-parentheses/最大括号匹配长度,括号是可以嵌套的#include #include #include #include using namespace std;class Solution {public: int longestValidParentheses(string s) { const int s_len = s.size(); stack indexstack; vector flagvector; int templ...
阅读全文
posted @ 2013-10-26 10:51
qingcheng奕
阅读(134)
推荐(0)