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 ...
随机推荐
- MyEclipse获取注册码
最近刚装上MyEclipse,一直弹窗提示注册码过期,开始还能接受,到最后,每发布一个项目便弹窗提醒,顿时感觉烦了,得治理治理这个烦人的注册码,下面是一段自动生成注册名和注册码的代码,只需要直接拿来用 ...
- redis与其可视化工具在win7上的安装
步骤 1.下载安装Redis服务. 2.调用执行文件创建服务器以及测试缓存. 3.使用可视化工具redis-desktop-manager管理查询缓存. 1.下载安装Redis服务. 下载地址:htt ...
- Dart 调用C语言混合编程
Dart 调用C语言本篇博客研究Dart语言如何调用C语言代码混合编程,最后我们实现一个简单示例,在C语言中编写简单加解密函数,使用dart调用并传入字符串,返回加密结果,调用解密函数,恢复字符串内容 ...
- 解决postman https请求无返回数据的问题
1.点击右上角的扳手图标 2.点击settings 3.点击general 4.把 ssl certificate verification这项点击关闭
- eas之设置单元格可编辑
for(int i=0;i<kdtEntrys.getRowCount();i++){ kdtEntrys.getRow(i).getCell("orgUnit").g ...
- Lua的string库函数、lua中string的模式匹配
--****************Lua的string库函数****************** --1.string.byte --string.byte (s [, i [, j]]) --取出 ...
- windows环境下用pip安装pyautogui遇到的几个问题
1.不能直接使用win+r运行cmd并使用pip,必须点击开始->windows系统->命令提示符,右键->以管理员身份运行 2.运行pip install pyautogui后提示 ...
- Git 基础教程 之 Git 安装 (windows)
一,安装Git,访问下面网址进行下载 https://www.git-scm.com/download/ 或者 https://pan.baidu.com/s/19imFBVHA2Yibmw1dyza ...
- Spring 属性输入和实例化操作_总结
Spring 之 applicationContext.xml 配置 默认实例化(无参构造) //被实例化对象 package cn.ibbidream.Bean; public class Bea ...
- HDU 4507
数位DP. 一般是利用DFS来求数位DP了,结合了记忆化搜索.设dp[i][j][k]为前i位,并且前i位的数位和mod7为j,前i位的数字的表示数字值mod7.为什么可以这样呢?因为继续DFS下去, ...