A. K-th Symbol in Grammar

Description

On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.

Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).

Example:
row 1: 0
row 2: 01
row 3: 0110
row 4: 01101001

思路

递归

Code

class Solution {
public:
int kthGrammar(int N, int K) {
if (N==1) return 0;
return kthGrammar(N-1, (K+1)/2) ? ((K%2)? 1 : 0) : ((K%2) ? 0 : 1);
}
};

B. Split BST

Description

Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the target value. It's not necessarily the case that the tree contains a node with value V.

Additionally, most of the structure of the original tree should remain. Formally, for any child C with parent P in the original tree, if they are both in the same subtree after the split, then node C should still have the parent P.

You should output the root TreeNode of both subtrees after splitting, in any order.

Example 1:

Input: root = [4,2,6,1,3,5,7], V = 2
Output: [[2,1],[4,3,6,null,null,5,7]]
Explanation:
Note that root, output[0], and output[1] are TreeNode objects, not arrays. The given tree [4,2,6,1,3,5,7] is represented by the following diagram: 4
/ \
2 6
/ \ / \
1 3 5 7 while the diagrams for the outputs are: 4
/ \
3 6 and 2
/ \ /
5 7 1

思路

递归

Code

class Solution {
public:
vector<TreeNode*> splitBST(TreeNode* root, int V) {
if (!root) {
vector<TreeNode*> ret = {NULL, NULL};
return ret;
}
if (root->val==V) {
vector<TreeNode*> ret = {root, root->right};
root->right = NULL;
return ret;
}
vector<TreeNode*> l = splitBST(root->left, V),
r = splitBST(root->right, V);
if (root->val < V) {
root->right = r[0];
vector<TreeNode*> ret = {root, r[1]};
return ret;
}
else {
root->left = l[1];
vector<TreeNode*> ret = {l[0], root};
return ret;
}
}
};

C. Swap Adjacent in LR String

Description

In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other.

Example:

Input: start = "RXXLRXRXL", end = "XRLXXRRLX"
Output: True
Explanation:
We can transform start to end following these steps:
RXXLRXRXL ->
XRXLRXRXL ->
XRLXRXRXL ->
XRLXXRRXL ->
XRLXXRRLX

思路

由规则,\(L\)只能一路向左移动,\(R\)只能一路向右移动。

所以,前后所有\(L\)与\(R\)的相对位置关系不变,且\(s2\)中\(L\)的位置只能在\(s1\)中对应的\(L\)的位置的左边,\(s2\)中\(R\)的位置只能在\(s1\)中对应的\(R\)的位置的右边。

Code

class Solution {
public:
struct node {
char c; int p;
};
bool canTransform(string start, string end) {
int len = start.size();
if (len != end.size()) return false;
vector<node> v1, v2;
for (int i = 0; i < len; ++i) {
if (start[i] != 'X') {
v1.push_back({start[i], i});
}
}
for (int i = 0; i < len; ++i) {
if (end[i] != 'X') {
v2.push_back({end[i], i});
}
}
int n = v1.size();
if (n != v2.size()) return false;
for (int i = 0; i < n; ++i) {
if (v1[i].c != v2[i].c) return false;
if (v1[i].c=='L') { if (v1[i].p < v2[i].p) return false; }
else { if (v1[i].p > v2[i].p) return false; }
}
return true;
}
};

D. Swim in Rising Water

Description

On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j).

Now rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to another 4-directionally adjacent square if and only if the total elevation of both squares is at most t. You can swim infinite distance in zero time. Of course, you must stay within the boundaries of the grid during your swim.

You start at the top left square (0, 0). What is the least time until you can reach the bottom right square (N-1, N-1)?

Example 1:

Input: [[0,2],[1,3]]
Output: 3
Explanation:
At time 0, you are in grid location (0, 0).
You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0. You cannot reach point (1, 1) until time 3.
When the depth of water is 3, we can swim anywhere inside the grid.
Example 2: Input: [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
Output: 16
Explanation:
0 1 2 3 4
24 23 22 21 5
12 13 14 15 16
11 17 18 19 20
10 9 8 7 6 The final route is marked in bold.
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.

思路

题意即 找一条(0, 0)到(n-1,n-1)的路径,上面的最大值最小

二分 最大值,然后判断连通性,用 记忆化搜索 即可。

Code

class Solution {
private:
bool vis[55][55], flag[55][55], temp[55][55];
bool can(int x, int y, int n, vector<vector<int>>& mp) {
if (vis[x][y]) return flag[x][y];
vis[x][y] = true;
if (!temp[x][y]) return flag[x][y] = false;
if ((x-1>=0&&can(x-1,y,n,mp)) || (x+1<n&&can(x+1,y,n,mp))
|| (y-1>=0&&can(x,y-1,n,mp)) || (y+1<n&&can(x,y+1,n,mp))) return flag[x][y] = true;
else return flag[x][y] = false;
}
bool ok(vector<vector<int>>& mp, int n, int x) {
memset(vis, 0, sizeof vis);
memset(flag, 0, sizeof flag);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (mp[i][j] > x) temp[i][j] = 0;
else temp[i][j] = 1;
}
}
if (mp[0][0] <= x) {
flag[0][0] = vis[0][0] = 1;
bool hh = can(n-1, n-1, n, mp);
return hh;
}
else return false;
}
public:
int swimInWater(vector<vector<int>>& grid) {
int n = grid.size(), minn = INT_MAX, maxx = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
minn = min(minn, grid[i][j]), maxx = max(maxx, grid[i][j]);
}
}
int l = minn, r = maxx;
while (l < r) {
int mid = l+r >> 1;
if (ok(grid, n, mid)) r = mid;
else l = mid+1;
}
return l;
}
};

LeetCode Weekly Contest 70 A B C D的更多相关文章

  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. centos7.4安装rabbitmq服务(3.7.10版本)

    一.需要安装erlang版本依赖,可以使用二进制安装方式,也可以通过rpm安装,但是安装的时候会提示需要erlang版本>=19.3,而且直接默认yum仓库中的版本较低.,为了节省时间,文章中直 ...

  2. 3D全景漫游

    全景图共分为三种: ①球面全景图 利用一张全景图围成一个球,自身位置位于球体内.由于图片是矩形,所以最上和最下的缝合处很明显就能够看得出来. 球面全景图是最接近人眼的构建模式,若利用多个立面构建,拼接 ...

  3. Java基础知识:Collection接口

    *本文是最近学习到的知识的记录以及分享,算不上原创. *参考文献见文末. 这篇文章主要讲的是java的Collection接口派生的两个子接口List和Set. 目录 Collection框架 Lis ...

  4. Django--源码安装

    1.安装setuptools cd /usr/src tar zxf setuptools-18.3.2.tar.gz cd setuptools-18.3.2/ python setup.py bu ...

  5. P2485 [SDOI2011]计算器

    P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...

  6. Redis实现之字符串

    简单动态字符串 Redis中的字符串并不是传统的C语言字符串(即字符数组,以下简称C字符串),而是自己构建了一种简单动态字符串(simple dynamic string,SDS),并将SDS作为Re ...

  7. 【ELK】ELK安装与配置

    一.ELK体系结构 二.系统环境变量 [主机信息] IP 主机名 操作系统版本 10.10.10.102 console CentOS7.5 10.10.10.103 log1 CentOS7.510 ...

  8. 使用WMI Filter 实现组策略的筛选!

    今天接到一个客户的一个问题,提到需要分系统版本分发相应的MSI程序.比如简体版接受简体版的分发程序,繁体版接受繁体版的分发程序!这个建立组策略的不同版本分发本身不会太难,我们只需要建立两个不同组策略分 ...

  9. CSU-2220 Godsend

    题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2220 题目 Description Leha somehow found ...

  10. sqlserver2008透明书库加密

    /*Title:TDE加密Author:浪客Environment:Windows Server 2008 Enterprise + SQL Server 2008 EnterpriseDescrip ...