LeetCode Weekly Contest 25
1. 507. Perfect Number
显然小于2的都不满足(尤其是负数的情况),进一步,显然质数都不满足,所以小于4的数,直接return false。
然后依次暴力枚举判断到sqrt(n),注意n = t * t的时候,t只需要加一次。好像不写这个也不会出错,因为没有这样的数满足条件。我没验证,需要的验证一下。
class Solution {
public:
bool checkPerfectNumber(int num) {
if(num <= ) return ;
int res = ;
int d = floor(sqrt(num));
for (int i = ; i <= d; i++) {
if(i == num) break;
if(num % i == ) {
res += i;
if(i != num / i) res += num / i;
if(res > num) return ;
}
}
return res == num;
}
};
2. 537. Complex Number Multiplication
按照题意完成就可以。
class Solution {
public:
string complexNumberMultiply(string a, string b) {
int x = , y = , m = , n = ;
sscanf(a.c_str(), "%d+%di", &x, &y);
sscanf(b.c_str(), "%d+%di", &m, &n);
stringstream ss;
int t1 = x * m - y * n, t2 = x * n + y * m;
ss << t1 << "+" << t2 << "i";
return ss.str();
}
};
3. 545. Boundary of Binary Tree
左扫一遍,叶子扫一遍,右边扫一遍,注意边界条件。编码比较多,注意特判。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> res;
vector<int> ri;
bool f;
void workleft(TreeNode* root) {
if(!root) return;
res.push_back(root->val);
if(root->left)
workleft(root->left);
else
workleft(root->right);
}
void work(TreeNode* root) {
if(!root) return;
if(!root->left && !root->right) {
if(!f) f = ;
else res.push_back(root->val);
}
work(root->left);
work(root->right);
}
void workright(TreeNode* root) {
if(!root) return;
ri.push_back(root->val);
if(root->right)
workright(root->right);
else
workright(root->left);
}
vector<int> boundaryOfBinaryTree(TreeNode* root) {
res.clear();
if(!root) return res;
res.push_back(root->val);
workleft(root->left);
if(root->left)
f = ;
else
f = ;
work(root->left);
work(root->right);
ri.clear();
workright(root->right);
reverse(ri.begin(), ri.end());
//cout << res.size() << " " << ri.size() << endl;
for (int i = ; i < ri.size(); i++) {
res.push_back(ri[i]);
//cout << ri[i] << endl;
}
return res;
}
};
4. 546. Remove Boxes
不会做,没有想法。看完题目,感觉跟burst ballon挺像的,因为枚举先删除的话,左右2边的会合在一起,使得后续的处理边的麻烦,应该是需要最后考虑,怎么进行合并,使得2边的问题变得独立。
从discuss里面看的答案。好像先把数组处理成字符,个数的形式,不是很好处理,需要寻找一种好的枚举方法,对问题进行化简。
刚开始,观察,如果一个字符只出现一次,那么这个字符对结果的贡献就是1,也就是说可以直接删除它,删除后左右2边的部分进行合并,这里有个问题,这个单独字符删除的时间,是否会影响最后的结果,不太确定。
我的想法是,处理成数字,个数的形式,然后依次枚举删除的字符。每次调用的时候,先进行单个字符的处理,然后合并相邻的,再递归调用,结果是tle。完全是暴力的解法。
下面的解法是,每次处理末尾的字符,使得问题规模缩小,然后考虑末尾的字符,可能跟前面的字符进行合并,然后进行搜索,有些段可能多次计算,采用记忆化搜索。这个想法的关键是,找到一种搜索的办法,缩减问题规模,同时不遗漏可能的解。
int dp[][][];
int work(vector<int>& b, int l, int r, int k) {
if(l > r) return ;
int& res = dp[l][r][k];
if(res != ) return res;
while(l < r && b[r - ] == b[r]) {
k++;
r--;
}
res = work(b, l, r - , ) + (k + ) * (k + );
for (int i = l; i < r; i++) {
if(b[i] == b[r]) {
res = max(res, work(b, l, i, k + ) + work(b, i + , r - , ));
}
}
return res;
}
int removeBoxes(vector<int>& boxes) {
memset(dp, , sizeof dp);
int n = boxes.size();
return work(boxes, , n - , );
}
LeetCode Weekly Contest 25的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
随机推荐
- (转)PostGIS+QGIS+GeoServer+OpenLayers实现数据的存储、服务的发布以及地图的显示
http://blog.csdn.net/gisshixisheng/article/details/41575833 标题比较长,主要呢是实现以下几点: 1.将shp数据导入到PostGIS中: 2 ...
- Functor and Monad in Swift
I have been trying to teach myself Functional Programming since late 2013. Many of the concepts are ...
- fileupload 上传控件
<div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:But ...
- 很实用的html meta标签实现页面跳转
就算你是有很多年开发经验的web开发工程师,有着很多web开发经验,对于先进的web开发技术有着很深刻的研究,然而你却忽略了那些最最基础的东西!现在我来问你,你是否对html所有的标签都能熟练的使用呢 ...
- BZOJ 3631: [JLOI2014]松鼠的新家 树上差分 + LCA
Description 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在“树”上.松鼠想邀 ...
- IDEA中使用Database管理工具
以下内容来自我的知乎回答IntelliJ IDEA中有什么让你相见恨晚的技巧? 说个冷门的,用IDEA操作数据库. 可能大部分不知道,IDEA是自带数据库管理工具的,类似于一个小型Navicat. 具 ...
- PAT_A1076#Forwards on Weibo
Source: PAT A1076 Forwards on Weibo (30 分) Description: Weibo is known as the Chinese version of Twi ...
- 01 c++常见面试题总结
https://www.cnblogs.com/yjd_hycf_space/p/7495640.html C++常见的面试题 http://c.tedu.cn/workplace/217749. ...
- 51nod1081 子段求和
给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和. 例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1.3 + 7 + 9 = ...
- PHP 设计模式之工厂模式 (静态工厂模式)
### 工厂模式: 由工厂类根据参数来决定创建出哪一种产品类的实例.工厂类是指包含了一个专门用来创建其他对象的方法的类.所谓按需分配,传入参数进行选择,返回具体的类.工厂模式的最主要作用就是对象创建的 ...