【错题集-编程题】重排字符串(贪心 + 构造)

牛客对应题目链接:重排字符串 (nowcoder.com)

力扣对应题目链接:1054. 距离相等的条形码 - 力扣(LeetCode)


一、分析题目


二、代码

1、没看题解之前AC的代码

//牛客代码(看了题解之后AC的代码)
#include <iostream>
#include <unordered_map>
using namespace std;

int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    string res=s;
    char maxValue=0;
    int maxCount=0;
    unordered_map<char, int> hash;
    for(auto x : s)
    {
        hash[x]++;
        if(hash[x]>maxCount)
        {
            maxCount=hash[x];
            maxValue=x;
        }
    }
    int k=0;
    while(maxCount--)
    {
        res[k]=maxValue;
        if(k>=n)
        {
            cout << "no" << endl;
            return 0;
        }
        k+=2;
    }
    hash[maxValue]=0;
    for(auto& [a, b] : hash)
    {
        while(b>0)
        {
            if(k>=n) k=1;
            res[k]=a;
            k+=2;
            b--;
        }
    }
    cout << "yes" << endl;
    cout << res << endl;
    return 0;
}

//力扣AC代码
class Solution {
private:
    unordered_map<int, int> hash;
public:
    vector<int> rearrangeBarcodes(vector<int>& barcodes) {
        int n=barcodes.size();
        vector<int> res(n);
        int maxValue=0;
        int maxCount=0;
        for(auto x : barcodes)
        {
            hash[x]++;
            if(hash[x]>maxCount)
            {
                maxCount=hash[x];
                maxValue=x;
            }
        }
        int k=0;
        while(maxCount--)
        {
            res[k]=maxValue;
            k+=2;
        }
        hash[maxValue]=0;
        for(auto& [a, b] : hash)
        {
            while(b>0)
            {
                if(k>=n) k=1;
                res[k]=a;
                k+=2;
                b--;
            }
        }
        return res;
    }
};

2、值得学习的代码

//牛客
#include <iostream>

using namespace std;

const int N = 100010;

int n;
char s[N];
char ret[N];

int main()
{
    cin >> n >> s;
 
    int hash[26] = { 0 }; // 统计每个字符的频次
    int maxIndex, maxCount = 0;
    for(int i = 0; i < n; i++)
    {
        if(maxCount < ++hash[s[i] - 'a'])
        {
            maxCount = hash[s[i] - 'a'];
            maxIndex = s[i] - 'a';
        }
    }
 
    if(maxCount > (n + 1) / 2) cout << "no" << endl;
    else
    {
        cout << "yes" << endl;
        int index = 0;
        // 先去摆放出现次数最多的
        while(maxCount--)
        {
            ret[index] = maxIndex + 'a';
            index += 2;
        }
        // 处理剩下的
        for(int i = 0; i < 26; i++)
        {
            if(hash[i] && i != maxIndex)
            {
                while(hash[i]--)
                {
                    if(index >= n) index = 1;
                    ret[index] = i + 'a';
                    index += 2;
                }
            }
        }
        // 打印结果
        for(int i = 0; i < n; i++) cout << ret[i];
        cout << endl;
    }
    return 0;
}

三、反思与改进

因为这道题目之前在力扣上做过类似的,所以基本思路很清晰,不过最后不知道是哪块细节没有处理好,导致只过了 33.33% 的样例。崩溃了,看了题解之后,发现是在正解前面少打印了一个 "yes",审题不仔细!!这种低级错误不能再犯,否则以后笔试会吃大亏!

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-29 21:52:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-29 21:52:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-29 21:52:01       82 阅读
  4. Python语言-面向对象

    2024-04-29 21:52:01       91 阅读

热门阅读

  1. python绘制三维散点图

    2024-04-29 21:52:01       35 阅读
  2. 嵌入式学习——C语言基础——day12

    2024-04-29 21:52:01       37 阅读
  3. Python学习路线图及开源库和工具推荐

    2024-04-29 21:52:01       24 阅读
  4. Seata分布式事务!!!

    2024-04-29 21:52:01       33 阅读
  5. 统计字符次数

    2024-04-29 21:52:01       36 阅读
  6. APP漏洞频发怎么办?渗透测试有用吗

    2024-04-29 21:52:01       34 阅读
  7. leetcode1146--快照数组

    2024-04-29 21:52:01       38 阅读
  8. 使用python写一个识别人脸

    2024-04-29 21:52:01       26 阅读
  9. C#面:委托是什么?事件是不是一种委托?

    2024-04-29 21:52:01       39 阅读
  10. 2d激光slam的改进方案探索

    2024-04-29 21:52:01       33 阅读
  11. C/C++中的整数乘法运算与汇编指令MUL和IMUL

    2024-04-29 21:52:01       35 阅读