我这次3000多名
感觉可以+个10分
https://i.imgur.com/9kYi4ea.png
附图为证
第一题:
要找目标旁边 或是斜角的元素和
思路:
照做
```cpp
class neighborSum {
public:
vector<vector<int>> paper;
neighborSum(vector<vector<int>>& grid)
{
paper = grid;
}
int adjacentSum(int value)
{
for(int i = 0 ; i < paper.size(); i ++)
{
for(int j = 0 ; j < paper[0].size() ; j ++)
{
if(paper[i][j] == value)
{
int res = 0;
if(i>0)res += paper[i-1][j];
if(i+1<paper.size())res += paper[i+1][j];
if(j>0)res +=paper[i][j-1];
if(j+1<paper[0].size())res += paper[i][j+1];
return res;
}
}
}
return 0;
}
int diagonalSum(int value)
{
for(int i = 0 ; i < paper.size(); i ++)
{
for(int j = 0 ; j < paper[0].size() ; j ++)
{
if(paper[i][j] == value)
{
int res = 0;
if(i>0 && j>0)res += paper[i-1][j-1];
if(i+1<paper.size() && j+1<paper[0].size())res += paper[i+
1]
[j+1];
if(j>0 && i+1<paper.size())res +=paper[i+1][j-1];
if(j+1<paper[0].size() && i>0)res += paper[i-1][j+1];
return res;
}
}
}
return 0;
}
};
/**
* Your neighborSum object will be instantiated and called as such:
* neighborSum* obj = new neighborSum(grid);
* int param_1 = obj->adjacentSum(value);
* int param_2 = obj->diagonalSum(value);
*/
```
第二题:
有n个城市
编号0 ~ n-1
他们i跟i-1之间的距离都是1
给你很多个queries
每个queries会有一条新的道路
同样距离都会是1
请问每加进一个新的道路之后
最短的路径是什么
思路:
因为资料比较少
所以直接用个图来模拟一下就好了
```cpp
class Solution {
public:
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queri
es
)
{
int len = queries.size();
vector<int> paper(n,0);
for(int i = 0 ; i < n ; i ++)
{
paper[i] = i;
}
vector<int> res(len,0);
unordered_map<int,vector<int>> save;
for(int i = 0 ; i < n-1 ; i ++)
{
save[i].push_back(i+1);
}
for(int i = 0 ; i < len ; i ++)
{
save[queries[i][0]].push_back(queries[i][1]);
for(int j = 0 ; j < n ; j ++)
{
for(int k : save[j] )
{
paper[k] = min(paper[k] , paper[j]+1);
}
}
res[i] = paper[n-1];
}
return res;
}
};
```
第三题:
图不会交叠
思路:
模拟
然后TLE
这应该有其他解法
放弃