2022-07-07 惨淡的蔚来面试
1.找错
#include<iostream>
using namespace std;
class A{
public:
virtual void a()=0;
A(){
cout<<"A ";
}
};
class B: public A
{
public:
B(){
cout<<"B ";
}
};
int main(){
A *a=new B();
return 0;
}
2.
class String
{
public:
String(const char *str = NULL){ // 普通构造函数
if(!str) {
m_data=nullptr;
m_data = '\0'
return;
}
len = strlen(str)
int len=0;
const char* p=str;
while(p){
len++;
p++;
}
len++;
m_data=malloc(len*sizeof(char));
const char* q=m_data;
strcpy()
p=str;
while(p){
*q++=*p++;
}
*q='\0';
}
String(const String &other){ // 拷贝构造函数(深拷贝 )
free(m_data);
m_data=other.m_data;
}
~ String(void){ // 析构函数
free(m_data);
}
String & operator =(const String &other){
return String(other.m_data);
}
private:
char *m_data; // 用于保存字符串
};
string str1= "helloworld";
string str2 = "aaaa"
str2 = str1;
3.
class String(
private:
String(){
data=malloc(1);
*data='\0';
}
char* data;
~String(){
free(data);
}
static bool initialized=false;
static mutex lock;
static String* instance;
public:
static String getSingleObject(){
// 加锁
if(initialized){
return *this;
}
// 解锁
this->String();
initialized=true;
return *this;
}
};
String::get
4.
题目描述:
主要元素
数组中占比超过一半的元素称之为主要元素。给定一个整数数组,找到它的主要元素。若没有,返回-1。
示例 1:
输入:[1,2,5,9,5,9,5,5,5]输出:5
示例 2:
输入:[3,2]输出:-1
示例 3:
输入:[2,2,1,1,1,2,2]输出:2
int findMoreThanHalf(vector<int> v){
int a=-1;
int counter=0;
for(const auto& i:v){
if(i==a) counter++;
else {
counter--;
if(counter<0){
a=i;
counter=1;
}
}
}
return counter?a:-1;
}
正式批做了笔试,没消息。
#蔚来面试##面试##23秋招##23届提前批#