摘要: Given an integer array of sizen, find all elements that appear more than⌊ n/3 ⌋times. The algorithm should run in linear time and in O(1) space.思路: 【... 阅读全文
posted @ 2015-07-01 20:01 tjuloading 阅读(291) 评论(0) 推荐(0)
摘要: Given an array of sizen, find the majority element. The majority element is the element that appears more than⌊ n/2 ⌋times.You may assume that the arr... 阅读全文
posted @ 2015-07-01 19:44 tjuloading 阅读(339) 评论(0) 推荐(0)
摘要: 思路: 通过增加一个辅助栈保存每个状态对应的最小值。栈实现的不完整,应该还包含empty()等常规函数。 1 #include 2 #include 3 using namespace std; 4 5 template class StackWithMin 6 { 7 public: 8... 阅读全文
posted @ 2015-06-23 14:03 tjuloading 阅读(154) 评论(0) 推荐(0)
摘要: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.For example, given the following matr... 阅读全文
posted @ 2015-06-22 20:08 tjuloading 阅读(148) 评论(0) 推荐(0)
摘要: 思路: 比较两个链表端点值的大小,通过递归的方式排列。 1 #include 2 using namespace std; 3 4 struct ListNode 5 { 6 int val; 7 ListNode *next; 8 ListNode(int v = 0... 阅读全文
posted @ 2015-06-22 15:20 tjuloading 阅读(179) 评论(0) 推荐(0)
摘要: 思路: 用三个指针preNode、curNode、nextNode完成。 1 #include 2 using namespace std; 3 4 struct ListNode 5 { 6 int val; 7 ListNode *next; 8 ListNode(... 阅读全文
posted @ 2015-06-21 15:47 tjuloading 阅读(143) 评论(0) 推荐(0)
摘要: 思路: 定义两个指针同时指向head,第一个指针先走K-1步,随后二个指针同时移动,当第一个指针到末尾处时,第二个指针所指向的即为倒数第K个结点。 1 #include 2 using namespace std; 3 4 struct ListNode 5 { 6 int val; ... 阅读全文
posted @ 2015-06-21 14:08 tjuloading 阅读(140) 评论(0) 推荐(0)
摘要: 思路: 头尾指针,向中间遍历,依据条件交换元素。 1 #include 2 using namespace std; 3 4 void reOrder(int *pData, unsigned int len, bool (*func)(int)) 5 { 6 if(pData == ... 阅读全文
posted @ 2015-06-20 16:58 tjuloading 阅读(227) 评论(0) 推荐(0)
摘要: 1 #include 2 using namespace std; 3 4 //构造链表结点 5 struct ListNode 6 { 7 int val; 8 ListNode *next; 9 10 ListNode(int v = 0):va... 阅读全文
posted @ 2015-06-20 15:13 tjuloading 阅读(166) 评论(0) 推荐(0)
摘要: 思路: 用n位字符数组表示n位数,通过递归的方式逐层(位)遍历,递归终止时打印。 1 #include "stdio.h" 2 #include "string.h" 3 4 //打印“数字” 5 void printNum(char* number) 6 { 7 //设置前导零状态为开... 阅读全文
posted @ 2015-06-19 21:52 tjuloading 阅读(169) 评论(0) 推荐(0)