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. 【技术累积】【点】【git】【10】.gitignore和.gitattributes

    .gitignore 告诉git忽略一些文件,git status会显示不到这些文件的状态. 一般放在项目根目录,以对全局控制,当然可以放在module下: 具体规则主要是: 以行为单位定义忽略文件类 ...

  2. 【sqli-labs】 less12 POST - Error Based - Double quotes- String-with twist (基于错误的双引号POST型字符型变形的注入)

    加个双引号 通过报错信息猜测SQL语句 , 将括号闭合掉,通过注释后面的条件登录

  3. C# 遍历文本框

    #region 文本框指定位置加入回车符 private void button1_Click(object sender, EventArgs e) { #region // 查询首字母位置 //s ...

  4. C# 网页内容获取

    private string GetGeneralContent(string strUrl) { string strMsg = string.Empty; try { WebRequest req ...

  5. MessageFormat.format()用法

    1.java.text.Format的继承结构如下   2.MessageFormat模式 FormatElement { ArgumentIndex }:是从0开始的入参位置索引 { Argumen ...

  6. yum的方式搭建mysql

    1.安装相应的软件yum install mysql : 安装mysql客户端 yum install mysql-server 安装服务端 yum install mysql-devel 安装相关的 ...

  7. C#第四节课

    分值语句(1) using System;using System.Collections.Generic;using System.Linq;using System.Text;using Syst ...

  8. vue.js 中 data, prop, computed, method,watch 介绍

    vue.js 中 data, prop, computed, method,watch 介绍 data, prop, computed, method 的区别 类型 加载顺序 加载时间 写法 作用 备 ...

  9. mysql如何删除数据库指定ID段的数据库。比如删除id 1-500的数据。

    delete from tablename where id>=1 and id<=500或者DELETE FROM `数据库名称`.`数据表名称` WHERE `house_cs`.`i ...

  10. js借助JSONP实现百度搜索框提示效果

    主要借助百度搜索的API,调用时会存在跨域问题,需要通过JSONP来解决这个问题,代码如下(代码中部分使用ES6语法): HTML <input type="text" id ...