写的有点晚了。

我每次都是先看一下这里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的更多相关文章

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

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

  2. 【LeetCode Weekly Contest 26 Q3】Friend Circles

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

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

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

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

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

  5. LeetCode Weekly Contest 8

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

  6. leetcode weekly contest 43

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

  7. LeetCode Weekly Contest 23

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

  8. Leetcode Weekly Contest 86

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

  9. LeetCode Weekly Contest

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

随机推荐

  1. dubbo之多注册中心

    Dubbo 支持同一服务向多注册中心同时注册,或者不同服务分别注册到不同的注册中心上去,甚至可以同时引用注册在不同注册中心上的同名服务.另外,注册中心是支持自定义扩展的. 多注册中心注册 比如:中文站 ...

  2. OAuth四种模式

    授权码模式(authorization code)----适用于网站服务端去oauth服务端申请授权 简化模式(implicit)----没有服务端,js+html页面去oauth服务端申请授权 密码 ...

  3. NSURLProtectionSpace 证书认证的上下文

    个NSURLProtectionSpace提供如下信息: //401的认证方式的realm字段的值 (NSString*)realm; //401的认证方式,指定是否密码发送安全. -(BOOL)re ...

  4. BZOJ2251 [2010Beijing Wc]外星联络 后缀数组 + Height数组

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in", "r", stdin ...

  5. 【剑指Offer】14、链表中倒数第k个结点

      题目描述:   输入一个链表,输出该链表中倒数第k个结点.为了符合习惯,从1开始计数,即链表的尾结点是倒数第1个节点.例如,一个链表有6个结点,从头结点开始,它们的值依次是1,2,3,4,5,6. ...

  6. 如何解决windows docker共享目录不支持符号链接(do not support symlinks)?

    windows使用docker toolbox,搭建前端开发环境时,在共享目录使用npm安装前端依赖时,发现报错,无法使用符号连接. 这里有一个帖子专门讨论这个问题,感兴趣可以看一下: https:/ ...

  7. [luogu1155 NOIP2008] 双栈排序 (二分图染色)

    传送门 Description Input 第一行是一个整数 n . 第二行有 n 个用空格隔开的正整数,构成一个 1−n 的排列. Output 共一行,如果输入的排列不是"可双栈排序排列 ...

  8. Linux思维导图之文件压缩

  9. phpcms v9 邓士鹏(石家庄职业技术学院)

    头部标题.关键词.描述调用: <title>{if isset($SEO['title']) && !empty($SEO['title'])}{$SEO['title'] ...

  10. 解决win10系统中将JDK1.8切换到JDK1.7时一直失败的问题

    场景:win10系统安装了1.7版本的JDK后,又安装了1.8版本的JDK,在后面的使用中想进行互相切换,但是1.8切换到1.7时一直失败   第一步:  删除 C:\Windows\System32 ...