LeetCode Weekly Contest 27
1. 557. Reverse Words in a String III
分割字符串,翻转。
class Solution {
public:
string reverseWords(string s) {
int n = s.size();
if(n < ) return s;
string res;
int i = ;
n += ;
s += " ";
int last = ;
while(i < n) {
last = i;
while(i < n && s[i] != ' ') i++;
string t = s.substr(last, i - last);
reverse(t.begin(), t.end());
res += t + " ";
i++;
}
return res.substr(, n - );
}
};
2. 554. Brick Wall
这刚好是以前做的hihocode的一道题目:https://hihocoder.com/problemset/problem/1494
就是统计边界的最大次数,然后n- max求出最小的穿过次数。
class Solution {
public:
int leastBricks(vector<vector<int>>& wall) {
int n = wall.size();
if(n == ) return ;
map<int, int> ma;
int res = ;
for (vector<int> &it:wall) {
int m = it.size();
int s = ;
for (int i = ; i < m - ; i++) {
s += it[i];
ma[s]++;
res = max(res, ma[s]);
}
}
return n - res;
}
};
3. 556. Next Greater Element III
要求下一个比较大的,可以采用next_permutation来做,然后判断下是不是真的比n大,然后还要check是否超过int的表示范围。
class Solution {
public:
int nextGreaterElement(int n) {
if(n < ) return -;
vector<int> v;
int t = n;
while(t > ) {
v.push_back(t % );
t /= ;
}
int sz = v.size();
//cout << sz << endl;
if(sz == ) return -;
reverse(v.begin(), v.end());
if(next_permutation(v.begin(), v.end())) {
long long res = ;
for (int t : v) {
res = res * + t;
}
if(res > INT_MAX)
return -;
return res;
} else
return -;
}
};
4. 549. Binary Tree Longest Consecutive Sequence II
这个题跟求二叉树的最长路径的方法是一致的,维护信息,以及更新结果。
我当时写的有点复杂,只需要维护每个点的最长上升和下降长度就行。注意:题目要求必须连续。
/**
* 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:
int res;
typedef pair<pair<int, int>, pair<int, int>> pp;
typedef pair<int, int> pii;
pair<pair<int, int>, pair<int, int>> work(TreeNode* root) {
if(!root) {
return {{,}, {, } };
}
if(!root->left && !root->right) {
int v = root->val;
res = max(res, );
return {{, v}, {, v} };
}
int v = root->val;
pp m1 = work(root->left), m2 = work(root->right);
int a1 = m1.first.first, a2 = m1.first.second, b1 = m1.second.first, b2 = m1.second.second;
int ta1 = m2.first.first, ta2 = m2.first.second, tb1 = m2.second.first, tb2 = m2.second.second;
int ra1 = , ra2 = v, rb1 = , rb2 = v;
if(v == a2 + ) {
ra1 = a1 + ;
}
if(v == ta2 + ) {
ra1 = max(ra1, ta1 + );
}
if(v == b2 - ) {
rb1 = b1 + ;
}
if(v == tb2 - ) {
rb1 = max(rb1, tb1 + );
}
if(v == a2 + && v == tb2 - ) {
res = max(res, + a1 + tb1);
}
if(v == ta2 + && v == b2 - ) {
res = max(res, + ta1 + b1);
}
res = max(res, max(ra1, rb1));
return {{ra1, ra2}, {rb1, rb2} }; }
int longestConsecutive(TreeNode* root) {
if(!root) return ;
res = ;
work(root);
return res;
}
};
LeetCode Weekly Contest 27的更多相关文章
- 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 ...
- 【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 ...
随机推荐
- ROS:Nvidia Jetson TK1平台安装使用ROS
原文连接: http://wiki.ros.org/indigo/Installation/UbuntuARM Ubuntu ARM install of ROS Indigo There are c ...
- 使用OpenCV画折线图
使用OpenCV画直方图是一件轻松的事情,画折线图就没有那么Easy了,还是使用一个库吧: GraphUtils 源代码添加入工程 原文链接:http://www.360doc.com/content ...
- rev
功能说明:反向输出文件内容. 字符串反转 文本反转
- Java继承实现接口的抽象类
1.TestIntace.java package com.chase.abstrac; /** * 接口 * @author Chase * * @date 2013-10-21 下午02:29:1 ...
- drf03 drf视图中提供的请求类和响应类
drf除了在数据序列化部分简写代码以外,还在视图中提供了简写操作.所以在django原有的django.views.View类基础上,drf封装了多个子类出来提供给我们使用. Django REST ...
- log日志模块得作用
import logginglogger=logging.getLogger()logger.setLevel(logging.DEBUG)#控制台输出日志'''consle=logging.Stre ...
- 【JavaScript】不使用正则表达式和字符串的方式来解析浏览器的URl地址信息
1.比如我们要获取的网站URl地址是:https://music.163.com/#/playlist?id=2384581760 一般我们能够想到的方式是直接使用正则表达式获取使用字符串直接解析的方 ...
- 使用GitHub代码仓库Repositories上传自己的项目代码
1.下载客户端github(必须下载,需要该软件所提供的Git shell输入命令来上传项目)下载地址: https://github-windows.s3.amazonaws.com/GitHubS ...
- 09.正则表达式re-2.complie函数
compile 函数用于编译正则表达式,生成一个 Pattern 对象,它的一般使用形式如下: import re # 将正则表达式编译成 Pattern 对象 pattern = re.compil ...
- 4.Spring注解+SpringMVC注解+MyBatis注解(动态sql)
1.创建如图所示项目结构 2.在项目的