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 ...
随机推荐
- 面试回答问题要防范hr的陷阱
面试技巧是大众化的,比较实在.在经历的各种面试中,最不容易防范的面试就是“拉呱”(山东方言,聊天的意思),这样子自己容易放松警惕. 看看下面的,就知道应该怎么应对这些“滑头”的HR! 1.我们为什么要 ...
- JSP_内置对象_session
session表示客户端与服务器的一次会话. Web中的session指的是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间. 从上述定义中可以看到,s ...
- python tips:matplotlib保存多张图片时,图片会相互叠加
问题: 使用matplotlib的pyplot.savefig保存图片时,前面的图会不断叠加到后面的图中. 原因: savefig方法保存图片并不会重置画布,所以导致图片的相互叠加. 解决方法: 保存 ...
- java中反射讲解及实例
Java反射机制详解 java 反射 定义 功能 示例 概要: Java反射机制详解 | |目录 1反射机制是什么 2反射机制能做什么 3反射机制的相关API ·通过一个对象获得完整的包名和类名 ·实 ...
- TensorFlow的序列模型代码解释(RNN、LSTM)---笔记(16)
1.学习单步的RNN:RNNCell.BasicRNNCell.BasicLSTMCell.LSTMCell.GRUCell (1)RNNCell 如果要学习TensorFlow中的RNN,第一站应该 ...
- 【剑指Offer】12、数值的整数次方
题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 解题思路: 本题看似比较简单,是一个简单的指数运算,但需要完 ...
- 为什么on用的时候会失效?
困扰了我一个很久的问题今天终于得带解决了,关于 on 的 用法: $("#hasLabels .link").on("click",function(){ .. ...
- python项目开发:ftp server开发
程序要求: 1.用户加密认证 (对用户名密码进行MD5验证)2.允许同时多用户登陆 (使用socket server方法,为每个用户都创建一个信息文件)3.每个用户有自己的家目录,且只能访问自己的家目 ...
- Codeforces 805A/B/C
A. Fake NP 传送门:http://codeforces.com/contest/805/problem/A 本题是一个数学问题. 给定两个正整数l,r(l≤r),对于区间[l..r]上的任一 ...
- 和socket、rs232通信遇到的糟糕问题。备查!
procedure Tfrm_FoldingCloth.btn1Click(Sender: TObject); var SND:array of byte; begin if Not cnrs232. ...