leetcode-第十二周
来源:互联网 发布:stc15单片机实战指南 编辑:程序博客网 时间:2024/06/06 02:16
331. Verify Preorder Serialization of a Binary Tree
class Solution {private: vector<string> getT(string preorder) { vector<string> ret; preorder += ","; for (int i = 0; i < preorder.size(); i++) { string tmp; int j = i; for (; j < preorder.size() && preorder[j] != ','; j++) tmp.push_back(preorder[j]); i = j; ret.push_back(tmp); } return ret; } bool dfs(const vector<string> &T, int &pos, int depth) { if (pos >= T.size()) return false; if (T[pos] == "#") return true; bool ret = true; ret &= dfs(T, ++pos, depth + 1); ret &= dfs(T, ++pos, depth + 1); return ret; }public: bool isValidSerialization(string preorder) { vector<string> T = getT(preorder); int pos = 0; dfs(T, pos, 0); cout << pos << " " << T.size() << endl; return pos == T.size() - 1; }};
330. Patching Array
/** * 这是贪心算法的一个应用。举个例子,对于数组 [1, 2, 3, 8] : * 1. 用一个miss来表示当前缺失的数,初始时为1,num[0] = 1,它的覆盖范围为 [1, 1] ,可以补足miss = 1 */class Solution {private: typedef long long LL;public: int minPatches(vector<int>& nums, int n) { int ret = 0; for (LL i = 0, miss = 1; miss <= n; ) { if (i < nums.size() && nums[i] <= miss) miss += nums[i++]; else miss += miss, ret++; } return ret; }};
0 0
- leetcode-python 第十二周
- leetcode-第十二周
- 第十二周leetcode题
- 第十二周LeetCode
- 第十二周LeetCode
- 第十二周LeetCode
- LeetCode题解 第十二周
- leetcode【第十二周】简化路径
- 第十二周LeetCode算法题三道
- Leetcode 算法习题 第十二周
- 第十二周:[leetCode] 152. Maximum Product Subarray
- leetcode第十二周解题总结--动态规划
- 第十二周 leetcode 207. Course Schedule(Medium)
- 第十二周
- 第十二周
- 第十二周
- 第十二周
- 第十二周
- angular2中父子组件的数据传递
- Console Pro 3 报错IndexOutOfRangeException: Array index is out of range. FlyingWormConsole3.FullSerial
- caffe-opencl 调用nvidia 错误 Aborted at 1494472487 (unix time) try "date -d @1494472487" if you are usin
- 11g R2 运行root.sh脚本不成功的一个例子:
- Android中将Activity转化为Dialog
- leetcode-第十二周
- android 老生常谈的权限问题
- php_语言结构
- 浅析Jquery里slick插件常用属性用途
- tomcat进程意外退出的问题分析
- Statement与PreparedStatement的区别
- # chcon -t samba_share_t /software chcon: can't apply partial context to unlabeled file /software
- 第17章 EXTI—外部中断/事件控制器—零死角玩转STM32-F429系列
- Windows 消息机制详解