最近在找暑期实习,受专业大坑的影像,自己学到的很多专业的知识很多都用不上,因为想从事程序员的工作,所以最近在进行知识的扫盲,同时也做了几家公司的笔试,整体感觉,第一题还行,但是后面的就有点力不从心了,特别是动态规划的题目,简直是一头雾水,下面贴一下最近的面试题目跟解法
#美团笔试//返回数组中第n大的数的和,其实可以用最大堆实现
class Solution{
public:
int findMax(vector<int> vec) {
int max = -999;
for (auto v : vec) {
if (max < v) max = v;
}
return max;
}
int getPositionOfMax(vector<int> vec, int max) {
auto distance = find(vec.begin(), vec.end(), max);
return distance - vec.begin();
}
int getRes(vector<int> &tem) {
vector<int> res(3);
int len = tem.size();
int sum = 0;
for (int i = 0; i < 3; i++) {
res[i] = findMax(tem);
int Maxposi = getPositionOfMax(tem, res[i]);
swap(tem[Maxposi], tem[len - 1]);
tem.pop_back();
}
for (int j = 0; j < res.size(); j++) {
sum = sum + res[j];
}
return (sum);
}
};
字节跳动
给一个原文:abcde,它对应的密码是:cbdae,就是中间那个是放第一位,然后按照中间左边,再中间右边的顺序
#define N 1000
class Solution{
public:
void decode(char aucPassword[]){
if(aucPassword==NULL)
return;
int len=strlen(aucPassword);
char s[len];
if(len%2>0){
s[len/2]=aucPassword[0];
int k=1;
for (int i = len/2; i>0 ;i--) {
s[i-1]=aucPassword[k];
s[len-i]=aucPassword[k+1];
k=k+2;
}
}else{
int k=0;
for (int i = len/2; i>=0 ;i--) {
s[i-1]=aucPassword[k];
s[len-i]=aucPassword[k+1];
k=k+2;
}
}
for (int j = 0; j<len ; j++) {
cout<<s[j];
}
string resEnd(s);
cout<<resEnd<<endl;
}
};
#阿里笔试题,统计字符串s1到s2的反转次数
class Solution{
public:
bool CheckPermutation(string s1, string s2) {
map<char, int> mp;
for (auto s : s1) mp[s]++;
for (auto s : s2) {
if (!mp[s]) return false;
mp[s]--;
}
for (auto t : mp) if (t.second) return false;
return true;
}
int findMinmove(string s1,string s2){
if(CheckPermutation(s1,s2)){
int res=0;
int len=s1.length();
int pos1=0,pos2=0;
while (pos1<len && pos2<len){
if(s1[pos1]==s2[pos2]){
pos1++;
pos2++;
} else
pos1++;
}
return(len-pos2);
}else{
return(-1);
}
}
};
#日企面试
//日企笔试,数组切片,公式,(n-1)*(n-2)/2
class Solution {
public:
int findequal_difference(vector<int>& A) {
int res = 0, len = 2, n = A.size();
for (int i = 2; i < n; ++i) {
if (A[i] - A[i - 1] == A[i - 1] - A[i - 2]) {
++len;
} else {
if (len > 2) res += (len - 1) * (len - 2) * 0.5;
len = 2;
}
}
if (len > 2) res += (len - 1) * (len - 2) * 0.5;
return res;
}
};
京东笔试
主要是实现股票的最小亏损,主要问题在数据的时间复杂度的处理上面,以及数据溢出的问题上,代码如下
// JD
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
void getMinloss(priority_queue<int>losData,int m,int n, int q){
int res=0;
int day=q/m,lis=q%m;
if(lis>0)
day=day+1;
if(q==n){
for (int i = 1; i <=day; ++i) {
for (int j = 0; j < m; ++j) {
res+=i*losData.top();
losData.pop();
if(losData.empty())
break;
}
}
} else{
for (int k = 0; k <n-q; ++k){
losData.pop();
}
for (int i = 1; i <=day; ++i) {
for (int j = 0; j < m; ++j) {
res+=i*losData.top();
losData.pop();
if(losData.empty())
break;
}
}
}
cout<<res<<endl;
}
int main() {
int n,m,q;
cin>>n>>m;
priority_queue<int> losData;
int temp;
for (int i = 0; i <n ; ++i) {
cin>>temp;
losData.push(temp);
}
cin>>q;
vector<int> qData;
for (int j = 0; j < q; ++j) {
cin>>temp;
qData.push_back(temp);
}
for (int k = 0; k <q; ++k) {
int s=qData[k];
getMinloss(losData,m,n,s);
}
return 0;
}
度小满 笔试
第一题是实现max pooling的过程,这个很简单,但是用c++写没法直接实现对矩阵的运算,所以还是两个循环进行查找,代码如下
// duxiaoman
#include<iostream>
#include<vector>
#include "Ddxiaoman.cpp"
using namespace std;
int arrangeDatadata(vector<vector<long>> s,int i,int j,int a,int b){
int max=s[i][j];
for (int k = i; k <a ; ++k) {
for (int l = j; l <b ; ++l) {
if(max<s[k][l]){
max=s[k][l];
}
}
}
return max;
}
int main()
{
int n,m,a,b;
cin>>n>>m>>a>>b;
vector<vector<long>> A;
int temp;
for (int k = 1; k <=n ; ++k) {
vector<long> a;
for (int i = 1; i <= m; ++i) {
int temp= (i * k) % 10;
a.push_back(temp);
}
A.push_back(a);
}
//定义输出的矩阵的长度与宽度
int outn= (n-a+1);
int outm=(m-b+1);
int res=0;
for (int l = 0; l < outn; ++l) {
for (int i = 0; i <outm ; ++i) {
res=res+arrangeDatadata(A,l,i,a,b);
}
}
cout<<res<<endl;
return 0;
}
网友评论