2021荣耀秋招笔试代码题

输入描述:

一个ASCII字符串,该字符串没有顺序,字符可以重复,输入字符串的长度<=30。

输出描述:

一个新的字符串,该字符串中字符在输入字符串中只出现一次,且该新字符串按照ASCII码从小到大的顺序排列。如果输入字符串中字符出现的次数都是大于1,则无需输出。

示例1:

输入:efghabcd1234efgh

输出:1234abcd

贴一下自己的代码,仅供参考,不一定准确。

#include <bits/stdc++.h>
using namespace std;
class Solution 
{
public:
    string test1(string Input)
    {
        int hashmap[128] = {0};
        string res;

        for(int i = 0; i < Input.length(); ++i)
        {
            hashmap[static_cast<int>(Input[i])]++;
        }
        for(int i = 0; i < 128; ++i)
        {
            if(hashmap[i] == 1)
            {
                res += static_cast<char>(i); 
            }
        }
        return res; 
    }
	string test2(string Input)
    {
        int hashmap[128] = {0};
        string res;

        for(int i = 0; i < Input.length(); ++i)
        {
            hashmap[Input[i] - '0']++;
        }
        for(int i = 0; i < 128; ++i)
        {
            if(hashmap[i] == 1)
            {
                res += i + '0'; 
            }
        }
        return res; 
    }
};

int main()
{
    char temp;
    string Input;
    Solution MySolution;
    
	cin >> Input;

    cout << MySolution.test1(Input) << endl;
	cout << MySolution.test2(Input) << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值