临近找工作,想记录下自己刷的题和一些结题方法,也算激励自己,希望看到的朋友也可以一起交流经验!
一、编程题
1. 小米c++编程题
给定一个长度为字符串, 需要去除所有之前曾经出现过的字符,只保留第一次出现的字符。
运行时间<1ms , 占用内存<200m
例如:
输入: pspexeawa
输出:psexaw
分析:先遍历主串,再遍历子串,判断主串中的元素与子串元素是否相等,不等则在字串中添加这个主串元素
代码实现:
#include <iostream>
#include <string.h>
#include <sstream>
#include <vector>
using namespace std;
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @return string字符串
*/
string unique_string(string s) {
// write code here
int len = strlen(s.c_str());
const char *p = s.c_str();
stringstream s_u;
vector<char> ss;
ss.push_back(*p);
while( *p != '\0')
{
int i=0;
while(*p != ss[i] && i != ss.size())
i++;
if(i == ss.size())
{
ss.push_back(*p);
}
p++;
}
for(auto ch : ss)
s_u << ch;
return s_u.str();
}
};
int main()
{
string s1 = "pspexeawa";
Solution solu;
string s2 = solu.unique_string(s1);
cout << s2 << endl;
return 0;
}
结果:
2. 猫狗队列
- 分析:
(1) 定义一个虚类Pet,引申出两个子类Dog和Cat (2) 定义链表队列的节点 (3) 定义链表队列的类PetList和其方法 (4) 利用简单工厂模式实例化子类
- 具体实现:
- Pet类
#include <iostream>
#include <string>
using namespace std;
class Pet{
public:
string type;
Pet() = default;
virtual ~Pet() = default;
virtual string getPetType() = 0;
};
class Dog : public Pet{
public:
Dog(string name) {
type = name;
}
string getPetType(){
return this->type;
}
};
class Cat : public Pet{
public:
Cat(string name) {
type = name;
}
string getPetType(){
return this->type;
}
};
- 子类实例化
class Pet_Factory{
public:
static Pet* create_pet(