面试试题天天练

本文记录了C++编程面试中的几道经典题目,包括去除重复字符、猫狗队列实现、滑动窗口最大值的计算以及链表反转的解题思路和代码实现,旨在分享解题经验和提升编程能力。
摘要由CSDN通过智能技术生成

临近找工作,想记录下自己刷的题和一些结题方法,也算激励自己,希望看到的朋友也可以一起交流经验!

一、编程题

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) 利用简单工厂模式实例化子类
  • 具体实现:
  1. 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;
        }
};
  1. 子类实例化
class Pet_Factory{
   
  public:
    static Pet* create_pet(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值