LeetCode Weekly Contest 26
写的有点晚了。
我每次都是先看一下这里http://bookshadow.com/leetcode/的思路,然后再开始写我自己的。
1. 521. Longest Uncommon Subsequence I
说实话,看完就懵逼了,这怎么做,难道是dp,后仔细一想,不对啊,很简单啊。
只要2个字符串不相等,返回长度较长的那个就行了。
刚开始,考虑,如果其中之一为空串,我返回的-1,不知道怎么想的。这显然是错的。
class Solution {
public:
int findLUSlength(string a, string b) {
int n = a.size(), m = b.size();
if(a == b)
return -;
return max(n, m);
}
};
2. 522. Longest Uncommon Subsequence II
有了第一题的基础,这道题就比较好分析。
注意到每个字符串长度至多是10,显然这是个入手点,考虑从这里入手。
方法是简单:先找长度最长的,然后如果这个字符是唯一的,那就返回这个长度就可以了,这个长度可能有很多字符串,找只出现一次的字符串,否则,找长度较短的。然后找到唯一的以后,还有一种可能,
这个较短是比较长的里面的子串,必须进行check。(我这里才想起来,比它长度长的,每个字符串至少出现2次,通过set去重,可以减少比较的次数,我没有进行这个优化)。然后就可以过了。
然后先按照长度串起来。
int f[];
class Solution {
public: int fd(int x) {
if(x == f[x]) return x;
return f[x] = fd(f[x]);
}
int findCircleNum(vector<vector<int>>& a) {
int n = a.size();
int res = n;
for (int i = ; i <= n; i++) f[i] = i;
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
if(a[i][j] == ) {
int x = fd(i), y = fd(j);
if(x != y) {
res--;
f[x] = y;
}
}
}
}
return res;
}
};
3. 547. Friend Circles
刚开始以为是连通分量,准备用dfs扫呢,突然发现邻接关系不是很好,然后转一思考,这不是并查集么,然后就欢快的写代码了。
最开始的链接里面有dsu(disjoint, find and union), dfs or bfs, floyd-warshall的解法。
class Solution {
public:
bool check(string &a, string & b) {
int n = a.size(), m = b.size();
int i, j;
i = j = ;
while(i < n && j < m) {
if(a[i] == b[j]) {
i++; j++;
} else {
i++;
}
}
return j == m;
}
int findLUSlength(vector<string>& s) {
int n = s.size();
if(n == ) return -;
if(n == ) return s[].size();
vector<string> a[];
for (string t : s) {
a[t.size()].push_back(t);
}
int res = ;
for (int i = ; i >= ; i--) {
if(a[i].size() == ) continue;
map<string, int> ma;
for (string t : a[i])
ma[t]++;
for (string t : a[i]) {
if(ma[t] == ) {
bool f = ;
for (int j = i + ; j <= ; j++) {
for (string d : a[j]) {
bool td = check(d, t);
if(td) {
f = ; break;
}
}
if(!f) break;
}
if(f) return i;
}
}
}
return -;
}
};
4. 548. Split Array with Equal Sum
看到这道题,是有点想法的,因为我见过,但是那时候不知道怎么做。其实不知道怎么做的原因有:1.题目不给数据范围,那就很尴尬啊,我得猜,怎么才能过,那时候只有半小时,没写代码,只是大概思考了一下怎么做,其实是帮别人做的。-w-.
看到数据范围2000,然后所有数据和还不会爆int,然后n^2的解法肯定是可以过的,那就大胆的写了。
显然,要求区间和,然后利用前缀和维护,这个预处理是必须的,然后可以O(1)的得到任意区间的长度。
然后,题意是去掉3个数,使得每一部分都相等,显然,数组长度至少为7,这得考虑清楚。然后考虑中间的那个需要去掉的数(为什么需要这么考虑,这里其实是有套路的,叫做meet in the middle, 叫做中间相遇攻击之类的),然后依次考虑每一边。
然后也是有问题,对于左边的每一次分割,我都要遍历右边么?这显然会导致n^3的复杂度,我就考虑把左右结果用set存下来,看看有没有相等的,有的话,就是满足的,没有的话,再次枚举。这样,复杂度降到logn。总的复杂度是n^2logn。
int s[];
class Solution {
public: int f(int x, int y) {
return s[y + ] - s[x];
}
bool splitArray(vector<int>& nums) {
int n = nums.size();
if(n < ) return ;
s[] = ;
for (int i = ; i <= n; i++) {
s[i] = nums[i - ] + s[i - ];
}
for (int i = ; i < n - ; i++) {
set<int> se;
for (int x = ; x < i - ; x++) {
if(f(, x - ) == f(x + , i - )) {
se.insert(f(, x - ));
}
}
if(se.size() == ) continue;
for (int x = i + ; x < n - ; x++) {
if(f(i + , x - ) == f(x + , n - )) {
if(se.count(f(i + , x - )))
//cout << f(i + 1, x - 1);
return ;
}
}
}
return ; }
};
其实这个题目是我同学报阿里内推的时候测试的题目,我那时候没有做出来。
我那时候的题目,就是utf-8的转化,跟leetcode 的uff-8 valied差不多,我没做出来。我还是有原因的:1.不给样例,到做完题目我都没搞懂什么意思。 2. 不给数据范围,长度是多少,根本不知道。我感觉都是靠我自己瞎猜的,看自己理解的对不对。
更滑稽的是:后来阿里面试的时候,面到一道题目,是varint,(我不懂是什么),题目描述,有一些数,比如long long,64位,但是经常出现几千,几万的数,很大的数比较少,然后要求你压缩一下,能不能节省空间。我没想法,没有做出来。后来一查,就是前面这个uft-8的题目,用每个byte的最高位标记是否还有更多的byte,是1的话,下一byte也是这个数的一部分,0的话,代表此数结束。然后代价是:大的数需要比它原本的占用空间要多。看完题解,我恍然大悟,这不就是测试的时候,那道题目么!
LeetCode Weekly Contest 26的更多相关文章
- 【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 ...
- 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 ...
随机推荐
- ASP.net获取当前url各种属性(文件名、参数、域名等)的方法
假设当前页完整地址是:http://www.test.com/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名 "www.te ...
- Visual Studio UI Automation 学习(三)
昨天了解到UI Automation是微软的.Net Framework框架里的4个DLL文件,可以在Visual studio里写代码时引入引用和引用命名空间.然后去写自动化代码. 今天本来是跟着一 ...
- 【汇编】dosbox钢琴
DATA SEGMENT msg DB 0DH,0AH,'[ 1 2 3 4 5 6 7 ]' DB 0DH,0AH,' [ q w e r t y u ]' DB 0DH,0AH,'________ ...
- Redis 之list链表结构及命令详解
1.lpush key value 从左放一个值 2.rpush key value 从右放一个值 3.lrange key start stop 获取链表数据(start ...
- C# 发起Get和Post请求
public class ApiHelper { //contentType application/json or application/xml public string HttpGet(str ...
- javascript 数组 常用方法
前言 学学忘忘 闲来做个笔记 整理下数组常用方法. Array 数组常用方法 创建数组的基本方式有两种 1.第一种是使用Array构造函数, var arr = new Array(); ...
- CSS学习笔记之CSS3新特性
目录 1.边框 2.背景 3.文本 4.字体 5.转换 6.过渡 7.动画 8.多列 9.自定义尺寸 CSS 用于控制网页的样式和布局,而 CSS3 是最新的 CSS 标准,这篇文章将着重介绍 CSS ...
- Spring Cloud-Zuul(十)
个人理解 在微服务体系体系中 我们会有很多服务.在内部体系中 通过eureka实现服务的自动发现通过ribbon实现服务的调用.但是如果对外部体系提供接口 我们就会涉及到接口的安全性,我们不能可能对每 ...
- 暑假集训D13总结
考试 又炸掉了= = 本来看着题就一脸茫然,默默的打暴力骗分,然后就交了卷= = 重要的是,在本机跑的毫无障碍的T3程序竟然在评测机CE啊喂,35分就没了啊喂(这可是比我现在分还高= =) 内心几近崩 ...
- poj 1274 基础二分最大匹配
#include<stdio.h> #include<string.h> #define N 300 #define inf 0x3fffffff int mark[N],li ...