每日OJ题_牛客_OR63删除公共字符_哈希_C++_Java

目录

牛客_OR63删除公共字符_哈希

题目解析

C++代码1

C++代码2

Java代码


牛客_OR63删除公共字符_哈希

删除公共字符_牛客题霸_牛客网 (nowcoder.com)


题目解析

用哈希表记录一下字符串的字符信息即可。

C++代码1

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

int main()
{
    string str1, str2;
    getline(cin, str1);
    getline(cin, str2);
    unordered_map<char, int> hash; // str2的字母和个数
    for(auto& e : str2)
    {
        hash[e]++;
    }
    string res;
    for(auto& e : str1)
    {
        if(!hash.count(e))
            res += e;
    }
    cout << res << endl;
    return 0;
}

C++代码2

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s, t;
    getline(cin, s);
    getline(cin, t);
    bool hash[300] = { 0 };
    for(char ch : t)
    {
        hash[ch] = true;
    }
    string ret;
    for(auto& ch : s)
    {
        if(!hash[ch])
        {
            ret += ch;
        }
    }
    cout << ret << endl;
    return 0;
}

Java代码

import java.util.Scanner;
import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        String t = br.readLine();
        boolean[] hash = new boolean[300];
        for(int i = 0; i < t.length(); i++)
        {
            hash[t.charAt(i)] = true;
        }
        for(int i = 0; i < s.length(); i++)
        {
            if(!hash[s.charAt(i)])
            {
                System.out.print(s.charAt(i));
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GR鲸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值