LeetCode Weekly Contest 20
1. 520. Detect Capital
题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断。还有:我感觉自己写的代码很丑,判断条件比较多,需要改进,精简再精简。
class Solution {
public:
bool detectCapitalUse(string word) {
int n = word.size();
if(n == ) return ;
if(word[] <= 'Z') {
bool f = ;
for (int i = ; i < n; i++) {
if(word[i] >= 'a') {}
else {
f = ; break;
}
}
if(f) return ;
f = ;
for (int i = ; i < n; i++) {
if(word[i] < 'a') {}
else {
f = ; break;
}
}
if(f) return ;
} else {
bool f = ;
for (int i = ; i < n; i++) {
if(word[i] < 'a') {
f = ; break;
}
}
if(f) return ;
}
return ;
}
};
2. 526. Beautiful Arrangement
看完题目,感觉数据范围很小,1<<15 = 32000,(这里不对,应该是15!,这个数字很大,暴力是不行的,之前的分析是错的!)比较小的,使用next_permutation暴力,枚举, 然后tle, 我就想着本地打表计算,直接输出结果,谁知道13,14,15的结果根本出不来。
后来看11,12的答案挺小,然后想着预处理出每个位置可以放的数字,然后dfs进行解的搜索,本地测试1-15,出解的速度很快,然后提交,就ac了。
class Solution {
public:
vector<int> e[];
bool in[];
int res;
int tn;
void work(int u) {
if(u > tn) {
bool f = ;
for (int i = ; i <= tn; i++) {
if(!in[i]) {
f = ; break;
}
}
res += f;
return;
}
for (int x : e[u]) {
if(in[x]) continue;
in[x] = ;
work(u + );
in[x] = ;
}
}
int countArrangement(int n) {
if(n < ) return n;
tn = n;
for (int i = ; i <= n; i++) {
e[i].clear();
for (int j = ; j <= n; j++) {
if(j % i == || i % j == )
e[i].push_back(j);
}
}
memset(in, , sizeof in);
work();
return res;
}
};
3. 525. Contiguous Array
这道题是原题,但是看完,想不出来递推方程,只好找以前的答案,幸好保存了,我是传送门http://www.cnblogs.com/y119777/p/5851025.html,多亏当初还仔细分析了一下,考虑了各种情况。
然后抄,提交,ac。
class Solution {
public:
int getLongestString(vector<int> & v) {
int n = v.size();
if(n < ) return ;
map<int, int> m;
int s = ;
int res = ;
for(int i = ; i < n; i++) {
s += v[i];
if(s == ) {
res = max(res, i + ); continue;
}
if(!m.count(s)) {
m[s] = i;
} else {
int len = i - m[s];
res = max(res, len);
}
}
return res;
}
int findMaxLength(vector<int>& nums) {
int n = nums.size();
if(n < ) return ;
for (int i = ; i < n; i++)
if(nums[i] == ) nums[i] = -;
return getLongestString(nums);
}
};
4。 517. Super Washing Machines
先看数据范围[0,1e4], [1, 1e5], 1e9,使用int,不会溢出。(时刻牢记这个坑,有时候最后结果不会溢出,但是,中间结果可能溢出)。n^2的复杂度有点高,而且。我也不知道n^2的解法怎么写!
一看hard,想不出思路,感觉要跪!这时候,一定提醒自己,静下心来,从最简单的例子入手,分析,有什么优秀的性质可以利用,考虑解法。
第一:不难想到,长度小于2,直接返回0,然后求和,结果不是长度的倍数,就直接返回-1,剩下的情况一定可以出结果。
第二:想不出n^2的方法,那就是复杂度为n的枚举方法,按这种枚举出来的结果,就是最优的。然后记 s = s / n; s为每个数字最后的目标,小于s的需要,至少需要s-a次,大于s的需要a-s次,然后考虑是否可以同时进行,由于每个数字互不干扰,每个数字每次移出只能移出1,所以只需要统计每个数字移出的次数,最后的结果就是最大的那个数字。仔细,思考一下,最好画一画。
class Solution {
public:
int findMinMoves(vector<int>& mach) {
int n = mach.size();
if(n < ) return ;
int s = ;
for (int x : mach)
s += x;
if(s % n != ) {
return -;
}
if(s == ) return ;
vector<int> c(n, );
s /= n;
int res = ;
for (int i = ; i < n; i++) {
int t = mach[i] + c[i];
if(t == s) {
} else if(t < s) {
c[i + ] -= s - t;
} else if(t > s) {
c[i] -= t - s;
mach[i + ] += t - s;
//c[i + 1] += t - s;
}
res = max(res, abs(c[i]));
//cout << c[i] << endl;
}
return res;
}
};
LeetCode Weekly Contest 20的更多相关文章
- 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 ...
随机推荐
- 压缩映射:简单最邻近搜索-(SLH)Simple Linear Hash
Compact Projection: Simple and Efficient Near Neighbor Search with Practical memory Requirement Auto ...
- 【sqli-labs】 less29 GET- Error based -Impidence mismatch -Having a WAF in front of web application (GET型基于错误的带有WAF注入)
这关有点意思,有一点需要事先注意,这关玩的是login.php而不是默认的index.php 再注入之前需要先了解一下HPP(HTTP Parameter Pollution),详情参照这篇 http ...
- sqlserver where in 在 mysql
) tmp); 主句(select * from (从句 temp) sql的 where in 删除 要更改为 // in( select * from ((select idfrom twhe ...
- C# 增加 删除 更新 方法
/// <summary> /// 增加一条数据 /// </summary> public int Add(string 表名,string 参数,string 参数值) { ...
- PS CC2018 命令大全
1.图像: 设置图像大小:图像->图像大小->设置宽高 约束比例: 解除约束比例: 2.设置大小像素图片不模糊: 双击当前图层->新建图层样式->输入名称->确定-> ...
- Selenium3+python自动化 单选框和复选框
一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了,可以先抽他了. 二.radio和 ...
- PKU 1019 Number Sequence(模拟,思维)
题目 以下思路参考自discuss:http://poj.org/showmessage?message_id=176353 /*我的思路: 1.将长串数分成一个个部分,每个部分是从1到x的无重复的数 ...
- HDU1087 - Super Jumping! Jumping! Jumping!【动态规划】
zh成功的在他人的帮助下获得了与小姐姐约会的机会,同时也不用担心被非"川大"的女票发现了,可是如何选择和哪些小姐姐约会呢?zh希望自己可以循序渐进,同时希望挑战自己的极限,我们假定 ...
- safari浏览器click事件要点击两次才有响应出现闪烁
闪烁问题 由于在iOS Safari上click事件存在300ms响应延时,所以为touch事件添加样式,会和click事件默认样式叠加而产生闪烁问题. 因为ios safari浏览器中对触摸事件的响 ...
- 支持移动触摸的jQuery图片Lightbox插件
简介 这是一款支持移动触摸设备的简洁jQuery图片Lightbox插件.该LightBox插件可以在移动手机和桌面设备中运行,它具有响应式,预加载图片,键盘支持等特点,非常实用.它的特点还有: 响应 ...