leetcode weekly contest 43

leetcode649. Dota2 Senate

leetcode649.Dota2 Senate

思路:

模拟规则round by round。

class Solution {
public:
string predictPartyVictory(string senate) {
int len = senate.size();
int r = 0;
int d = 0;
int i = 0;
while(1)
{
if(senate[i] == 'R')
{
int j = 1;
while(senate[(i + j) % len] != 'D' && j < len) j++;
if(senate[(i + j) % len] == 'R') return "Radiant";
else senate[(i + j) % len] = '#';
}
else if(senate[i] == 'D')
{
int j = 1;
while(senate[(i + j) % len] != 'R' && j < len) j++;
if(senate[(i + j) % len] == 'D') return "Dire";
else senate[(i + j) % len] = '#';
}
i = (i + 1) % len;
} }
};

leetcode650. 2 Keys Keyboard

leetcode650. 2 Keys Keyboard

思路:

找到最大的约数,dp。

注意copy算一步。

class Solution {
public:
int find(int x)
{
for(int i = 2; i <= sqrt(x); i++)
{
if(x % i == 0) return i;
}
return x;
}
int minSteps(int n) {
vector<int> res(n + 1, 0);
int temp;
for(int i = 2; i <= n; i++)
{
temp = find(i);
res[i] = res[i / temp] + temp;
}
return res[n];
}
};

leetcode651. 4 Keys Keyboard

leetcode651. 4 Keys Keyboard

思路:

dp.

res[n] = max{res[n - 1] + 1, res[n - 5] * 4, res[n - 4] * 3, res[n - 3] * 2};

时间O(n),空间O(1)

class Solution {
public:
int maxA(int N) {
//vector<int> res(N + 1);
int four,three,two,one,five,res;
five = four = three = two = one = res = 0;
for(int i = 1; i <= N; i++)
{
res = max(one + 1,max(four * 3, max(three * 2,five * 4)));
five = four;
four = three;
three = two;
two = one;
one = res;
}
return res;
}
};

leetcode652. Find Duplicate Subtrees

leetcode652. Find Duplicate Subtrees

思路:

这个思路我感觉特别地棒。 既然是找duplicates,可以直接用map。吧树给serialize了变成一个string,让map自己比较。然后找到全部的duplicates。

时间O(n)

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
unordered_map<string,vector<TreeNode*> > map;
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
vector<TreeNode*> res;
serialize(root);
unordered_map<string,vector<TreeNode*>>::iterator it = map.begin();
while(it != map.end())
{
if(it->second.size() > 1 && it->first != "")
{
res.push_back(it->second[0]);
}
it++;
}
return res;
}
private:
string serialize(TreeNode* root)
{
if(!root) return "";
string s = '(' + serialize(root->left) + to_string(root->val) + serialize(root->right) + ')';
map[s].push_back(root);
return s;
}
};

leetcode weekly contest 43的更多相关文章

  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 23

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

  3. Leetcode Weekly Contest 86

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

  4. LeetCode Weekly Contest

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

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

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

  6. 【LeetCode Weekly Contest 26 Q3】Friend Circles

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

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

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

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

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

  9. LeetCode Weekly Contest 117

    已经正式在实习了,好久都没有刷题了(应该有半年了吧),感觉还是不能把思维锻炼落下,所以决定每周末刷一次LeetCode. 这是第一周(菜的真实,只做了两题,还有半小时不想看了,冷~). 第一题: 96 ...

随机推荐

  1. 《STL源码剖析》读书笔记

    转载:https://www.cnblogs.com/xiaoyi115/p/3721922.html 直接逼入正题. Standard Template Library简称STL.STL可分为容器( ...

  2. Linux内存高,触发oom-killer问题解决

    最近遇到两起Linux的内存问题,其一是触发了oom-killer导致系统挂 1. 首先确认该系统的版本是32位 ? #uname -a Linux alarm 2.6.9-67.ELsmp #1 S ...

  3. Shp上传至Oracle Spatial

    1.下载shp2sdo,将shp文件拷贝至shp2sdo相同路径下,打开windows命令窗口,执行: shp2sdo shp文件名 表名 -i id -s 4326 -d 例如:shp2sdo ci ...

  4. cenos6.5安装vsftp

    1.安装 yum install vsftpd 2.配置 vi /etc/vsftpd/vsftpd.conf 检查是否如下配置 anonymous_enable=NO #禁止匿名访问 chroot_ ...

  5. fedroa20 没法开启ntpd服务器

    1现象:ntpd老是没法开启,ntpd -d显示有个进程占用123端口. [root@vd13crmtb01 ~]# systemctl enable ntpd.service         //开 ...

  6. Jmeter----5.1 设置中文

    注意:JMeter5需要Java8 以上,本文环境是Win7 64位 设置永久默认汉化:在Jmeter的安装目录下的bin目录中找到 jmeter.properties这个文件,用文本编辑器打开.在# ...

  7. 以Docker容器方式安装Ceph

    获取Ceph的Docker镜像 因为公司对于网络环境的限制,安装ceph时使用ceph-deploy反而很不方便,且ssh免密码方式也不适用,所以使用docker方式安装. Git地址 https:/ ...

  8. Model Binder

    上面需检测id是否为null 如果未提供id值或id值类型不匹配,则使用默认值.但仍需校验值是否超出范围    

  9. 【LOJ】#2544. 「JXOI2018」游戏

    题解 九条可怜还有那么善良的一面??? 显然有些数在这个区间里没有数是它的约数,它们其中的最后一个取的一定就是\(t(p)\)的值 这样我们只需要枚举\(t(p)\)的值,这个值就是"没有任 ...

  10. PHP学习笔记(一)数组

    初始化数组的方法:统一初始化或逐项初始化. 遍历数组的方法: 1.FOR循环 2.DO...WHILE语句 3.WHILE语句 4.foreach foreach($arr as $key=>$ ...