随笔分类 -  OJ

摘要:二叉树的层序遍历思路一:利用队列,将每一层节点放入队列,各层节点之间加入NULL隔开。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 ... 阅读全文
posted @ 2015-04-04 18:59 bournet 阅读(111) 评论(0) 推荐(0)
摘要:如果一个链表有环,求环的起始位置。设置一个快指针每次走两步,一个慢指针每次走一步。若最终能相遇,则有环。如图:设:链表头是X,环的第一个节点是Y,slow和fast第一次的交点是Z。各段的长度分别是a,b,c,如图所示。环的长度是L。第一次相遇时slow走过的距离:a+b,fast走过的距离:a+b... 阅读全文
posted @ 2015-04-04 16:43 bournet 阅读(179) 评论(0) 推荐(0)
摘要:二分查找 1 class Solution { 2 public: 3 int findMin(vector &num) { 4 int n = num.size(); 5 if (n == 1) 6 return num[0]; 7 ... 阅读全文
posted @ 2015-04-03 00:14 bournet 阅读(105) 评论(0) 推荐(0)
摘要:问题:判断二叉树是否为镜像二叉树分析:递归判断,根节点单独判断,然后递归左结点和右结点,之后每次一起递归左结点的左结点和右结点的右结点比较,左结点的右结点和右结点的左结点比较。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { ... 阅读全文
posted @ 2015-04-02 23:21 bournet 阅读(121) 评论(0) 推荐(0)
摘要:主要是对下标的操作。 1 class Solution { 2 public: 3 vector > generateMatrix(int n) { 4 vector > ret(n, vector(n)); 5 6 int i = 0, j... 阅读全文
posted @ 2015-04-01 19:13 bournet 阅读(118) 评论(0) 推荐(0)
摘要:题目要求是找到一组数组的全排列思路1:递归,在num中拿出1个数字放在第一个,然后剩下的数字做一个全排列 1 class Solution { 2 public: 3 vector > permute(vector &num) { 4 int n = num.size(); ... 阅读全文
posted @ 2015-04-01 14:40 bournet 阅读(181) 评论(0) 推荐(0)
摘要:题目的意思是整个过程中只能买一只股票然后卖出,也可以不买股票。也就是我们要找到一对最低价和最高价,最低价在最高价前面,以最低价买入股票,以最低价卖出股票。分析一:扫描一遍,找到最大增长即可。从前往后,用当前价格减去此前最低价格,就是在当前点卖出股票能获得的最高利润。扫描的过程中更新最大利润和最低价格... 阅读全文
posted @ 2015-03-14 20:18 bournet 阅读(177) 评论(0) 推荐(0)
摘要:给出一个已排序的数组,将其转化为二叉查找树(BST)。思路:取数组中间元素为根结点的value,则数组左侧、右侧分别为BST的左子树、右子树。递归可求解。代码如下: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * ... 阅读全文
posted @ 2014-11-26 16:14 bournet 阅读(149) 评论(0) 推荐(0)
摘要:根据二叉树的前序遍历和中序遍历构造二叉树。思路:前序遍历的第一个节点就是根节点,扫描中序遍历找出根结点,根结点的左边、右边分别为左子树、右子树中序遍历。再计算左子数长度leftLength,前序遍历根结点后的leftLength长度为左子树的前序遍历,剩下的为右子树的前序遍历,代码如下: 1 /**... 阅读全文
posted @ 2014-11-26 10:48 bournet 阅读(134) 评论(0) 推荐(0)
摘要:问题描述:n个台阶,每次只能跨1个或者2个,求有多少种方法。ways[i]:i个台阶的方法。可能是由i-1个台阶跨1个或者由i-2个台阶,转化为斐波那契数列,即1 int climbStairs(int n)2 {3 if (n == 1)4 return 1;5 e... 阅读全文
posted @ 2014-11-20 09:57 bournet 阅读(136) 评论(0) 推荐(0)
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,... 阅读全文
posted @ 2014-11-19 16:43 bournet 阅读(109) 评论(0) 推荐(0)
摘要:题目https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/对每个节点递归。 1 /** 2 * Definition for binary tree with next pointer. 3 * ... 阅读全文
posted @ 2014-11-19 11:07 bournet 阅读(134) 评论(0) 推荐(0)
摘要:Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1... 阅读全文
posted @ 2014-11-18 15:50 bournet 阅读(130) 评论(0) 推荐(0)
摘要:判断一个链表是否为循环链表。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x... 阅读全文
posted @ 2014-10-27 21:58 bournet 阅读(140) 评论(0) 推荐(0)
摘要:将一块很长的木板切割成N块,长度分别为L1、L2、…、LN。每次切割需要的开销为当前木板的长度。求出将木板切割完最小开销是多少。在N块木板中取最短和次短的相加,加到开销中,再在N-1块木板中重复此操作,直到木板数为1。 1 #include 2 #include 3 #include 4 us... 阅读全文
posted @ 2014-10-08 10:28 bournet 阅读(180) 评论(0) 推荐(0)
摘要:给定长度为N的字符串S,构造长度为N的字符串T,起初T是空串,反复从S的头部或者尾部删除一个字符,加到T的尾部。目标是构造字典序尽可能小的T。尝试如下贪心算法:不断取S头部和尾部较小的字符放到T的尾部。考虑S头部和尾部字符相同的情况。有如下算法:按照字典序比较S和将S反转后的字符串S‘;如果S较小,... 阅读全文
posted @ 2014-09-30 17:29 bournet 阅读(404) 评论(0) 推荐(0)
摘要:有一个大小为N*M的园子,雨后积水。八连通的积水被认为是连接在一起的,求出园子里共有多少水洼(八连通是下图中相对W的*部分)。* * **W** * *分析:从任意的W开始,不停地把邻接的部分用'.'代替。1次dfs后与初始的W连接的所有的W都被替换成'.',即这个水洼消失了。因此直到图中不存在W为... 阅读全文
posted @ 2014-09-27 11:05 bournet 阅读(340) 评论(0) 推荐(0)
摘要:1 #include 2 #include 3 using namespace std; 4 5 int main() 6 { 7 string input; 8 while (getline(cin, input)) 9 {10 int Quicks... 阅读全文
posted @ 2014-09-17 11:34 bournet 阅读(106) 评论(0) 推荐(0)
摘要:1 #include 2 #include 3 using namespace std; 4 5 int main() 6 { 7 int N = 1000000; 8 bool prime[N+1]; 9 for (int i = 0; i > a >> d >> ... 阅读全文
posted @ 2014-09-16 10:34 bournet 阅读(146) 评论(0) 推荐(0)
摘要:1 #include 2 #include 3 #include 4 using namespace std; 5 6 int main() 7 { 8 int N = 400; 9 //input用来存储输入数据10 int input[N][N];11 ... 阅读全文
posted @ 2014-09-15 23:55 bournet 阅读(169) 评论(0) 推荐(0)