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的更多相关文章

  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 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  3. LeetCode Weekly Contest 23

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

  4. Leetcode Weekly Contest 86

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

  5. LeetCode Weekly Contest

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

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

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

  7. 【LeetCode Weekly Contest 26 Q3】Friend Circles

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

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

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

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

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

随机推荐

  1. AI:IPPR的数学表示-CNN结构进化(Alex、ZF、Inception、Res、InceptionRes)

    前言: 文章:CNN的结构分析-------:  文章:历年ImageNet冠军模型网络结构解析-------: 文章:GoogleLeNet系列解读-------: 文章:DNN结构演进Histor ...

  2. OpenCV:OpenCV中的 parallel_for 和opencv parallel_for_

    OpenCV使用OMP完成并行运算,在使用AdaBoost检测的时候,在cascadedetect.cpp 里面,大量使用 parallel_for_(Range(0, stripCount), Ca ...

  3. 07--c++类的构造函数详解

    c++类的构造函数详解 c++构造函数的知识在各种c++教材上已有介绍,不过初学者往往不太注意观察和总结其中各种构造函数的特点和用法,故在此我根据自己的c++编程经验总结了一下c++中各种构造函数的特 ...

  4. (转)基于Metronic的Bootstrap开发框架经验总结(9)--实现Web页面内容的打印预览和保存操作

    http://www.cnblogs.com/wuhuacong/p/5147368.html 在前面介绍了很多篇相关的<Bootstrap开发框架>的系列文章,这些内容基本上覆盖到了我这 ...

  5. Metadata

    元数据是关于数据的组织.数据域及其关系的信息,简言之,元数据就是关于数据的数据. Metadata is "data [information] that provides informat ...

  6. eoLinker AMS 专业版V3.3发布:分享项目可以测试并选择分享内容等

    eoLinker AMS是集API文档管理.API自动化测试.开发协作三位一体的综合API开发管理平台,是中国最大的在线API管理平台.目前eoLinker AMS已经为来自全球的超过两万家企业托管超 ...

  7. eas设置时分秒

  8. NOIp2016-NOIp2011解题报告(骗分)

    zxl钦点.让我练暴力骗分. 那就把2016-2011年的题目搞一搞. NOIp2016 Day1 T1 AC 100pts. (妈呀,这么水的一道题竟然还要调试,一遍过不了样例,果然是要退役的节奏啊 ...

  9. ro的session

    1.需要session管理器,根据需要选择: 2.需要一个login和logout的服务.注意:设定TRORemobteDataMedule.sessionmanager,但要设定TRORemobte ...

  10. TCriticalSection(Delphi)

    临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别 临界区对象TCriticalSection(Delphi) 与 TRtlCritical ...