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. 【shell】shell编程(五)-读取参数

    通过前几篇文章的学习,我们学会了shell的基本语法.在linux的实际操作中,我们经常看到命令会有很多参数,例如:ls -al 等等,那么这个参数是怎么处理的呢? 接下来我们就来看看shell脚本对 ...

  2. 给vim安装YouCompleteMe

    要安装YouCompleteMe ,vim须支持python.看是否支持,可以在vim中:version 查看, 如果python前有+号,就是支持,减号就是不支持. 如果不支持,需要以编译安装方式重 ...

  3. Msql中的触发器

    解发器 当执行某种操作时解发的行为. 比如, 当表变动时触发的动作. 像商城订单, 当下单时, 库存减少. 语法: create trigger trigger_name after/befor in ...

  4. django框架<三>

    一.ORM操作  1.django orm创建数据库的方法 (1)指定连接pymysql(python3.x),先配置__init__.py import pymysql pymysql.instal ...

  5. Arm-kernel 内存收集【转】

    转自:http://blog.csdn.net/linyt/article/details/6627664 Linux kernel的内存管理子系统非常复杂,为了深入了解内存管理系统,我打算分多篇文章 ...

  6. shell 监控磁盘使用率【转】

    方案一: disks=(`df |sed 1d | awk '{print $1,$5}'|tr -d %`) len=${#disks[@]} ;i<=$len;i=i+));do ];the ...

  7. 数据库连接池(c3p0与druid)

    1.数据库连接池概念 其实就是一个容器(集合),存放数据库连接的容器.当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归 ...

  8. Linux Shell基础篇——变量

    一.Shell中的变量 注:这里所说的Shell是Bash Shell,我姑且统称为Shell. Shell中的变量分为用户自定义变量.环境变量.位置参数变量.预定义变量.在Shell中,变量的默认类 ...

  9. Web APi入门之Self-Host(二)

    这篇来讲讲WebApi的自托管,WebApi可以托管到控制台/winform/服务上,并不是一定要依赖IIS才行. 1.首先新建控制台项目,在通过Nuget搜索Microsoft.AspNet.Web ...

  10. chrome浏览器使用HTML5预览图片

    chrome浏览器对HTML5支持的较好,使用HTML5的File相关的api,可以实现前台页面在选定图片后,不上传即可预览.代码如下: 1.前台代码,使用<input type="f ...