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 ...
随机推荐
- Verification之PSL之intro
1 PSL - Property specification language 1.1 Property - Characteristics of the designs/verification e ...
- SQL Server对数据进行添加
下面介绍一下SQL Server如何往数据库中加入数据. <!-- 添加主人信息 --> <form action="add_Admin.jsp" method= ...
- VTK嵌入MFC同步显示
使用VTK嵌入MFC,实现四视图更新,机制和细节参考原文. 原文链接:http://blog.csdn.net/www_doling_net/article/details/8939115 原文代码: ...
- Visual Basic for Application
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'The note of Visual Basic for Applicati ...
- 【sqli-labs】 less19 POST - Header Injection - Referer field - Error based (基于头部的Referer POST报错注入)
这个和less18一样,都是基于header的注入 这次的字段是referer Referer: ' AND UpdateXml(1,concat(0x7e,database(),0x7e),1),1 ...
- js 判断是否为数字
if (data.value === "" || data.value == null) { return false; } console.log(!isNaN(data.val ...
- webpack核心提炼
基本是学习的时候在网上整理的资料,并非自己原创,这篇文章的的主要目的是记录webpack.config.js的配置方式.可能也有不少错误,欢迎指正!! 一.应用场景 前端模块化开发.功能拓展.css预 ...
- Apex语言(四)选择(决策)结构
1.选择结构 选择结构是当满足某个条件或不满足某个条件时,需要进行决策以控制执行的流程. 2.if语句 if语句由布尔表达式后跟一个或多个语句组成. [格式] if(条件表达式){ 语句: } [流程 ...
- python 动态修改 类和实例 的方法
相信很多朋友在编程的时候都会想修改一下已经写好的程序行为代码,而最常见的方式就是通过子类来重写父类的一些不满足需求的方法.比如说下面这个例子. class Dog: def bark(self): p ...
- PAT_A1138#Postorder Traversal
Source: PAT A1138 Postorder Traversal (25 分) Description: Suppose that all the keys in a binary tree ...