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的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  3. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  4. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  5. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  6. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  7. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  8. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  9. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

随机推荐

  1. 经典实用SQL Server语句大全总结(一)

    简要介绍基础语句:1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创 ...

  2. 关于layui 下拉框 bug

    @for (; i < ; i++) { <option value=</option> } 当value=""时候 自动添加选中样式

  3. jq 禁用复选框 和输入框

    $('input').attr("readonly", ""); $('input').attr("disabled", "fal ...

  4. BZOJ 4817: [Sdoi2017]树点涂色 LCT+Access的性质+DFS序+线段树

    Code: #include<bits/stdc++.h> #define maxn 200003 #define inf -1000000 using namespace std; vo ...

  5. 复习MySQL②数据类型及约束条件

    数据类型分为数值类型.日期和时间类型.字符串类型 数值类型: – INT:有符号的和无符号的.有符号大小-2147483648~2147483647,无符号大0~4294967295. 宽度最多为11 ...

  6. Selenium的定位元素

    1.浏览器操作 # 刷新 driver.refresh()   # 前进 driver.forward()   # 后退 driver.back() 2.获取标签元素 # 通过ID定位目标元素 dri ...

  7. [转载]查看Linux系统硬件信息实例详解

    linux查看系统的硬件信息,并不像windows那么直观,这里我罗列了查看系统信息的实用命令,并做了分类,实例解说. cpu lscpu命令,查看的是cpu的统计信息. blue@blue-pc:~ ...

  8. 被遗忘的 Logrotate

    转自: http://huoding.com/2013/04/21/246 被遗忘的 Logrotate 发表于 2013-04-21 我发现很多人的服务器上都运行着一些诸如每天切分 Nginx 日志 ...

  9. uwsgi部署django,里的request调用的接口响应慢解决方法

    解决方法,增加2个线程 uwsgi.ini 配置如下 chdir=/var/www/Ultramanpidfile=/tmp/uwsgi.pidmodule=Ultraman.wsgimaster=t ...

  10. CentOS7 笔记 (一) .NETCore

    安装系统CentOS,虚拟机好麻烦,直接在阿里云开了一个6个月免费的ECS. 熟悉Linux 基本命令 登录,exit,vi ,vim,vi保存关闭,w,ls,mkdir,df,ip addr,修改系 ...