写的有点晚了。

我每次都是先看一下这里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. SQL Server之存储过程

    存储过程的概念 存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行. 存储过程中可以包含逻辑控制语句和数据操纵语句,它 ...

  2. What is the difference between PKCS#5 padding and PKCS#7 padding

    The difference between the PKCS#5 and PKCS#7 padding mechanisms is the block size; PKCS#5 padding is ...

  3. sql server time(7) 默认值

    语句为 ALTER TABLE dbo.YourTable ADD CONSTRAINT DF_TimeDefault DEFAULT '00:00:00' FOR YourTimeColumn 比如 ...

  4. 【笔记】Linux就该这么学-第六课第四章

    vim使用    a 在光标后一位置插入    i 在光标当前位置插入    o 在光标下面创建个空行    dd 删除(剪切)光标所在行    5dd 删除(剪切)从光标处开始的5行    yy 复 ...

  5. 【剑指Offer】6、旋转数组的最小数字

      题目描述:   把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5 ...

  6. swift-导航栏添加自定义返回按钮

    //1.添加返回按钮 func addBackBtn(){ let leftBtn:UIBarButtonItem=UIBarButtonItem(title: "返回", sty ...

  7. 数据结构实验病毒感染检测问题(C++)

    医学研究者最近发现了某些新病毒,通过对这些病毒的分析,得知他们的DNA序列都是环状的.现在研究者已收集了大量的病毒DNA和人的DNA数据,想快速检测出这些人是否感染了相应的病毒.为了方便研究,研究者将 ...

  8. [luogu3230 HNOI2013] 比赛 (搜索+Hash)

    传送门 Solution 搜索加Hash记录状态,记忆化搜索,需要注意顺序无关答案 Code //By Menteur_Hxy #include <map> #include <cm ...

  9. VmWare安装centos7无法上网

    1.关闭防火墙 systemctl stop firewalld.service #关闭 systemctl restart firewalld.service #重启 2.虚拟机->设置-&g ...

  10. 2.2 SVN的简单使用

    1.打开SVN服务器 选中Repositories→右键→Create new Repositories 选中Test2→右键→Copy URL to Clipboard 打开记事本粘贴地址:http ...