- 博客(22)
- 资源 (3)
- 收藏
- 关注
原创 Leetcode 486. Predict the Winner
dp[i][j]表示i到j区间先手拿最终最多能拿多少分,sum[i][j]表示区间i到j的和,那么状态转移方程:dp[i][j] = max(nums[i] + sum[i+1][j] - dp[i+1][j], nums[j] + sum[i][j-1] - dp[i][j-1])class Solution {public: bool PredictTheWinner(vecto
2017-09-10 00:10:22
173
原创 Leetcode. Add to List 310. Minimum Height Trees
一个节点的”高度”是由距离最远的叶子节点决定的,单独计算一个节点的“高度”是O(n)的,计算所有的节点就是O(n^2)的,这其中有很多重复的工作,但是并不是很容易优化。特殊的是所有叶子节点的“高度”都是1,而第二层叶子节点的“高度”是2,依次增加,直到树的中央,我想这也是样例输入没有把树画成传统样子的原因,就是在提示树中央的节点就是最深的节点,也是“高度”最大的节点。所以,先找到所有叶子节点(
2017-09-09 23:22:00
191
原创 leetcode 524. Longest Word in Dictionary through Deleting (自定义sort排序cmp)
这题没什么难度,主要训练自定义sort排序熟练程度,还有双指针的熟练程度。class Solution {public: static bool cmp(const string &a, const string & b) { if (a.size()!=b.size()) { return a.size()>b.size();
2017-06-03 21:42:29
280
原创 leetcode 43. Multiply Strings
字符串整数乘法,考虑按位相乘,就是小学时用笔演算的方法,从两个数的个位开始算,i,j,两两相乘,更新结果对应位置的数字,注意更新的数字不止与i,j的乘积有关(cur),还与这位之前算的数字(digit)和上次计算的十位(last)有关,更新公式是digit = (digit+cur%10+last)%10,完事还要更新新的last。另外还有个小坑是进位到最后可能会多出来一位,所以索性在num1和n
2017-06-03 13:50:01
169
原创 leetcode 547. Friend Circles
用一个数组标记每个人是否在目前形成的所有圈内。dfs挨个搜出每个人进来时是否在之前形成的圈内,在的话总圈数不变,不在的话总圈数+1,然后相应维护更新所有圈的情况。class Solution {public: int dfs(int i, vector>& M, vector& v) { if (v[i] == true) return 0;
2017-04-05 16:14:16
389
原创 leetcode 337. House Robber III
搜到一个节点时有两种选择:1、取该节点的val和隔一层后面的搜索结果之和2、不取该节点val,取相邻下一层的搜索结果比较两种情况取较大的。优化:用map存好每个节点的结果,减少重复工作。class Solution {public: unordered_map m; int rob(TreeNode* root) { if (root ==
2017-04-05 00:14:07
341
原创 leetcode 542. 01 Matrix
借助队列层次遍历,相当于从每个0的地方同步地一层一层向外搜,这样保证距离最近的0是第一个先搜过来的,后面来的只要检查之前是否有搜过来即可。class Solution {public: vector> updateMatrix(vector>& matrix) { int m = matrix.size(), n = matrix[0].size();
2017-04-04 23:25:18
276
转载 c++知识点随记
下面哪一个不是动态链接库的优点?A.共享B.装载速度快C.开发模式好D.减少页面交换静态链接与动态链接:1 静态链接库的优点 (1) 代码装载速度快,执行速度略比动态链接库快; (2) 只需保证在开发者的计算机中有正确的.LIB文件,在以二进制形式发布程序时不需考虑在用户的计算机上.LIB文件是否存在及版本问题,可
2017-04-01 13:39:24
265
原创 queue 简单实用
1 queueint> q;2 q.push(1); //向队尾加入元素3 q.front(); //取队头元素4 q.pop(); //删除队头元素
2017-04-01 13:34:46
187
原创 hihocoder 1198 Memory Allocating Algorithm
1 #include 2 #include 3 #include 4 #include string.h> 5 #include 6 #include 7 #include 8 #include set> 9 #include 10 #include 11 #define LL long long12 13 using namespace std;14 int
2017-04-01 13:34:43
174
原创 hihocoder 1197 Give My Text Back
1 #include 2 #include 3 #include 4 #include string.h> 5 #include 6 #include 7 #include 8 #include set> 9 #include 10 #include 11 12 using namespace std;13 14 15 char buf[10000];16
2017-04-01 13:34:38
752
原创 结构体优先队列的用法
今天做一个微软的校招笔试题 Registration Day ,用优先队列模拟操作的。粘贴来别人的代码,谨记 pq 的用法。另外 memset 包含在 string.h 里。 1 #include 2 #include string.h> 3 #include 4 #include 5 #include 6 #include set> 7 #include 8 #inc
2017-04-01 13:34:33
1098
原创 int 和 unsigned int
正常32位机器的 int 和 unsigned int 都是32位长,int 范围 -2147483648~2147483647,正数存原码,负数存补码。(1)-6的二进制: 1000 0000 0000 0110(称为原码,原码是计算机显示给我的)(2)对原码求反码:1111 1111 1111 1001(称为反码,保持符号位不变,将原码中的0变1,1变0)(3)对反码加1:1111 1111
2017-04-01 13:34:28
1343
原创 Leetcode: Pacific Atlantic Water Flow
417. Pacific Atlantic Water Flow题意大概是说输入2维 int 矩阵,数值表示高度,规定水只能朝上下左右四个方向流,输出同时满足1、能够流到矩阵的上边缘或者左边缘;2、能够流到矩阵的下边缘或者右边缘的所有坐标点。简单分析之后觉得这是一道 dp 题,写的飞起,一个 dp 表从左上角开始更新满足条件1的点,只需要检查能流到左边(或上边)且左边(或上边)满足1则该点也更
2017-04-01 13:34:24
176
原创 Leetcode: Matchsticks to Square
473. Matchsticks to Square题意大致是给定一个整数组,判断是否可以分组成4组等和的。先求和除四,算出每组的大小 Len,如果不能被4整除的就直接返回啦。然后问题变成了数组能不能分成和为 Len 的4组。直接 brute force 的话需要做4遍检索,这里考虑用 pair 存每个数出现的次数,然后采用先降序排列再dfs,dfs时能取大数尽量取大数,这样组成4组Len就
2017-04-01 13:34:19
209
原创 数值优化学习之概览(一)
问题:优化一个目标函数f(x),满足一些约束c(x),等式或者不等式。满足约束的解成为可行解。分类:连续/离散优化问题约束/非约束优化问题线性/非线性优化问题全局/局部优化问题随机/确定性优化问题凸优化:1、凸集:如果集合S为凸集,当且仅当 x∈S, y∈S 并且α(x)+(1−α)(y) inS;α∈[0,1]2、凸函数:凸函数:如果函数f(x)为凸函数,当且仅当S为凸集,x
2017-04-01 13:34:14
344
原创 平方根倒数速算法
今天看《python科学计算》学习到的平方根倒数速算法这里实践并记录:由于刚刚接触py不久,首先查了一下自定义函数用timeit模块计算时间性能的用法如下, 1 import random 2 import timeit 3 from time import clock 4 5 6 def get_random_number(num): 7 '''get random
2017-04-01 13:34:09
573
原创 LeetCode:Single Number
Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extr
2017-04-01 13:34:05
190
原创 LeetCode:Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times. Exam
2017-04-01 13:34:00
166
原创 LeetCode:Verify Preorder Serialization of a Binary Tree
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
2017-04-01 13:33:56
138
原创 LeetCode:Restore IP Address
93. Restore IP AddressesGiven a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135",return ["255.255.11.135",
2017-04-01 13:33:51
230
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人