题目A
https://leetcode-cn.com/problems/longest-nice-substring/
因为
l
e
n
g
t
h
≤
100
length \leq 100
length≤100,我们直接就可以遍历子串然后更新答案。
class Solution {
public:
bool Check(string s)
{
unordered_set<int> m;
for (auto u : s)
m.insert(u);
for (auto u : m)
{
if (u >= 'a' && m.count(u - 32) < 1) // 97 - 62 = 32
return false;
else if (u <= 'Z' && m.count(u + 32) < 1)
return false;
}
return true;
}
string longestNiceSubstring(string s) {
string ret = "";
for (int i = 0; i < s.size(); i ++ )
{
for (int j = i + ret.size(); j < s.size(); j ++ )
{
if (Check(s.substr(i, j - i + 1)))
{
ret = s.substr(i, j - i + 1);
}
}
}
return ret;
}
};
题目B
https://leetcode-cn.com/problems/form-array-by-concatenating-subarrays-of-another-array/
因为
l
e
n
g
t
h
≤
1000
length \leq 1000
length≤1000,我们直接就可以直接
O
(
N
2
)
O(N^2)
O(N2)的复杂度就解决该问题。
直接对
n
u
m
s
nums
nums数组遍历,遍历的同时审核是否可以凑出来
g
r
o
u
p
s
groups
groups数组
class Solution {
public:
vector<vector<int> > gps;
vector<int> nums;
bool Check(int st, int x)
{
for (int j = 0; j < gps[x].size(); j ++ )
{
if (st + j >= nums.size() || gps[x][j] != nums[st + j])
return false;
}
return true;
}
bool canChoose(vector<vector<int>>& groups, vector<int>& _nums) {
nums = _nums;
gps = groups;
int j = 0;
for (int i = 0; i < nums.size(); i ++ )
{
if (Check(i, j))
{
if (j == gps.size() - 1)
return true;
i += gps[j].size() - 1; // 减去 1 是因为后面有 ++
j ++;
}
}
return false;
}
};
题目C
https://leetcode-cn.com/problems/map-of-highest-peak/
直接就是一个 bfs 就可以了,主要越界情况,和**vector<vector >**的快速初始化
#define x first
#define y second
typedef pair<int, int> PII;
class Solution {
public:
int n, m;
bool Check(int a, int b)
{
return a >= 0 && b >= 0 && a < n && b < m;
}
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
n = isWater.size(), m = isWater[0].size();
queue<PII> que;
vector<vector<int> > ret(n, vector<int>(m, -1));
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (isWater[i][j] == 1)
que.push(PII(i, j)), ret[i][j] = 0;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
PII tmp; int x, y;
while (que.size())
{
tmp = que.front(); que.pop();
for (int i = 0; i < 4; i ++ ) {
x = tmp.x + dx[i], y = tmp.y + dy[i];
if (Check(x, y) && ret[x][y] == -1) {
ret[x][y] = 1 + ret[tmp.x][tmp.y];
que.push(PII(x, y));
}
}
}
return ret;
}
};
题目D
https://leetcode-cn.com/problems/tree-of-coprimes/
有一个很玄学的问题,就是
T
M
TM
TM
vector copr[55];定义在全局变量那里就会超时,然而类的内部就很快,奇奇怪怪
对于本题的思路就是根据 nums 数字范围太小,我们可以将互质数字进行枚举是否在dfs的路径上。
dfs的路径就是我们根的路径,很方便
const int N = 100010, M = N * 2;
int h[N], e[M], ne[M], idx;
int pos[55];
int depth[N];
class Solution {
public:
int n;
vector<int> w;
vector<int> ret;
vector<int> copr[55];
inline int gcd(int x, int y) {
if (y == 0)
return x;
else
return gcd(y, x % y);
}
void add(int x, int y) {
e[idx] = y, ne[idx] = h[x], h[x] = idx ++;
}
void dfs(int u, int fa) {
int val = w[u];
for (auto & x : copr[val])
if (pos[x] != -1)
if (ret[u] == -1 || depth[ret[u]] < depth[pos[x]])
ret[u] = pos[x];
int tmp = pos[val];
pos[val] = u;
int v;
for (int i = h[u]; ~i; i = ne[i]) {
v = e[i];
if (v == fa) continue;
else {
depth[v] = depth[u] + 1;
dfs(v, u);
}
}
pos[val] = tmp;
}
vector<int> getCoprimes(vector<int>& nums, vector<vector<int>>& edges) {
for (int i = 1; i <= 50; i ++ )
for (int j = 1; j <= 50; j ++ )
if (gcd(i, j) == 1)
copr[i].push_back(j);
n = nums.size(), w = nums;
memset(h, -1, sizeof h), idx = 0;
memset(pos, -1, sizeof pos);
memset(depth, 0, sizeof depth);
for (auto & t : edges)
add(t[0], t[1]), add(t[1], t[0]);
ret.resize(n, -1);
depth[0] = 0;
dfs(0, -1);
return ret;
}
};