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. SmartUpload实现文件上传

    (一)SmartUpload组件简介 SmartUpload组件 专门用于实现文件上传及下载的免费组件   (二)SmartUpload组件特点 使用简单:编写少量代码,完成上传下载功能 能够控制上传 ...

  2. 时序分析:ARIMA模型(非平稳时间序列)

    转载于一篇硕士论文.... ARIMA模型意为求和自回归滑动平均模型(IntergratedAut少regressive MovingAverageModel),简记为ARIMA(p,d,q),p,q ...

  3. sql_3 join

    http://www.cnblogs.com/rush/archive/2012/03/27/2420246.html  

  4. 一个完整的Appium手机自动化测试实例

    实现过程: 1.使用环境 appium .安卓SDK .python 本文重点是自动化实例,环境搭建过程省略. 2.找到被测APP的包名和Activity Name 手机连接上电脑后,在DOS环境先使 ...

  5. Linux 僵尸进程如何处理

    Linux 允许进程查询内核以获得其父进程的 PID,或者其任何子进程的执行状态.例如,进程可以创建一个子进程来执行特定的任务,然后调用诸如 wait() 这样的一些库函数检查子进程是否终止.如果子进 ...

  6. appium分层自动化的封装

    1.创建一个case包,start_app的python文件 #coding=utf-8from appium import webdriverfrom util.read_init import R ...

  7. 06.系统编程-4.多线程和GIL

    为什么有人会说 Python? 多线程是鸡肋?知乎上有人提出这样一个问题,在我们常识中,多进程.多线程都是通过并发的方式充分利用硬件资源提高程序的运行效率,怎么在 Python 中反而成了鸡肋? 有同 ...

  8. 7.ES几种常见的搜索方式

    主要知识点  1, query string search (1)  GET /ecommerce/product/_search (2) GET/ecommerce/product/_search? ...

  9. canvas实现圆框图片

    作者:issac_宝华链接:http://www.jianshu.com/p/9a6ee2648d6f來源:简书 在html中做圆框图片很容易,只需要简单的 border-radius: 50%; 当 ...

  10. 与公司2位经理的交流,Web开发知识库建设

    1.代码库3种类型 WebCommon:网站开发技术选型和最佳实践 FansCommons :各种可以复用的代码 CentronCore,CentronWeb 3种类型:通用,web,环境(通用+We ...