//二维vector
vector<vector<int> > vec(n , vector<int>(m));
//从小到大
priority_queue <int,vector<int>,greater<int> > q;
//从大到小
priority_queue <int,vector<int>,less<int> >q;
//重载运算符
bool operator < (node a,node b) {
return a.x<b.x;
}
在网上看到了网易的笔试题,自己没有参加,随便写了写,不一定对
第一题
思路:就是一个简单的bfs
class solution {
public:
int dx[4] = {0,0,1,-1};
int dy[4] = {-1,1,0,0};
int minSailCost(vector<vector<int> >& input) {
if(input.empty() || input[0].empty()) return -1;
int n = input.size() ,m=input[0].size();
vector<vector<int> > cost(n,vector<int>(m));
vector<vector<bool> > vis(n,vector<bool>(m));
for (int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
cost[i][j] = 0x3f3f3f3f3f3f3f;
vis[i][j] = false;
}
}
queue<pair<int,int> > index;
index.push({0,0});
cost[0][0] = 0, vis[0][0]= true;
pair<int,int> now;
while(!index.empty()) {
now = index.front();
index.pop();
vis[now.first][now.second] = false;
for(int i=0;i<4;i++) {
int nex_x = now.first + dx[i];
int nex_y = now.second + dy[i];
if(nex_x<0||nex_x>=n) continue;
if(nex_y<0||nex_y>=m) continue;
if(input[nex_x][nex_y] == 2) continue;
int nex_cost = 0;
if(input[nex_x][nex_y] == 0) nex_cost =cost[now.first][now.second]+2;
else nex_cost =cost[now.first][now.second]+1;
if(cost[nex_x][nex_y]<=nex_cost) continue;
//这部分可以让花费更少
cost[nex_x][nex_y] = nex_cost;
if(vis[nex_x][nex_y] == false) {
index.push({nex_x,nex_y});
vis[nex_x][nex_y] = true;
}
}
}
return cost[n-1][m-1] == (int)0x3f3f3f3f3f3f3f ? -1:cost[n-1][m-1];
}
};
第二题
思路:使用一个priority_queue<myData,vector, greater > sortedAge; 每次放出的都是最小的数,然后查看自己左右两边的位置有没有被占据,如果被占据了,说明有旁边有比自己小的小孩,纸张要比他们多,然后就OK了
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> myData;
int main() {
vector<int> peopleAge;
int personAge;
char c;
while(scanf("%d%c",&personAge,&c)) {
peopleAge.push_back(personAge);
if(c=='\n') break;
}
int n = peopleAge.size();
priority_queue<myData, vector<myData >, greater<myData > > sortedAge;
vector<int> vis(n);
for(int i=0;i<vis.size();i++) {
vis[i] =0;
sortedAge.push({peopleAge[i],i});
}
int ans=0;
while(!sortedAge.empty()) {
myData now = sortedAge.top();
sortedAge.pop();
int l = (now.second-1+n)%n ,r = (now.second+1)%n;
int mx = 1;
if(vis[l]) {
if(peopleAge[l] == peopleAge[now.second]) mx = max(mx, vis[l]);
else mx = max(mx, vis[l]+1);
}
if(vis[r]) {
if(peopleAge[r] == peopleAge[now.second]) mx = max(mx, vis[r]);
else mx = max(mx, vis[r]+1);
}
vis[now.second] = mx;
ans+=mx;
}
printf("%d",ans);
}
第三题
思路:就是递归。如果是在中间,就输出(char)(n+'a'-1).再把字符串分成左右两边,如果位置k在左边,就输出findChar(n-1,k);如果k在位置右边,就输出invert(findChar(n-1,kk)),其中kk是左边的位置,与k向对应。
class solution {
public:
int stringSize[30];
char invert(char c) {
int now = c-'a';
char a= 25 - now;
a = a+'a';
return a;
}
char findChar(int n,int k) {
if(k == stringSize[n]) return (char)(n+'a'-1);
if(k < stringSize[n]) {
return findChar(n-1, k);
}
else {
k = (stringSize[n]<<1)-k;
return invert(findChar(n-1, k));
}
}
char findKthBit(int n,int k) {
stringSize[1] = 1;
for(int i=2;i<=4;i++) stringSize[i] = (stringSize[i-1]<<1)+1;
for(int i=1;i<=4;i++) {
stringSize[i] = (stringSize[i]+1)>>1;
}
return findChar(n, k);
}
};