校招算法笔面试 | 华为机试-字符统计

题目

题目链接

解题思路

这是一个字符统计和排序问题。需要统计字符串中每个字符出现的次数,并按照以下规则排序:

  1. 按照字符出现次数由多到少排序
  2. 如果出现次数相同,则按照ASCII码由小到大排序

关键点

  1. 输入规则:

    • 字符串长度: 1 ≤ l e n ( s t r ) ≤ 1000 1 \leq len(str) \leq 1000 1len(str)1000
    • 只包含小写英文字母和数字
  2. 处理要求:

    • 统计每个字符出现次数
    • 按照出现次数降序排序
    • 出现次数相同时按ASCII码升序排序

代码

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

int main() {
    string str;
    while (cin >> str) {
        // 统计每个字符出现次数
        vector<pair<char, int>> counts;
        vector<bool> visited(128, false);  // ASCII码范围
        
        for (char c : str) {
            if (!visited[c]) {
                visited[c] = true;
                int count = 0;
                for (char ch : str) {
                    if (ch == c) count++;
                }
                counts.push_back({c, count});
            }
        }
        
        // 排序:先按次数降序,次数相同按ASCII码升序
        sort(counts.begin(), counts.end(), 
             [](const pair<char, int>& a, const pair<char, int>& b) {
                 if (a.second != b.second) return a.second > b.second;
                 return a.first < b.first;
             });
        
        // 输出结果
        string result;
        for (const auto& p : counts) {
            result += p.first;
        }
        cout << result << endl;
    }
    return 0;
}
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.nextLine();
            
            // 统计每个字符出现次数
            Map<Character, Integer> countMap = new HashMap<>();
            for (char c : str.toCharArray()) {
                countMap.put(c, countMap.getOrDefault(c, 0) + 1);
            }
            
            // 转换为列表便于排序
            List<Map.Entry<Character, Integer>> list = 
                new ArrayList<>(countMap.entrySet());
            
            // 排序:先按次数降序,次数相同按ASCII码升序
            Collections.sort(list, (a, b) -> {
                if (!a.getValue().equals(b.getValue())) {
                    return b.getValue() - a.getValue();
                }
                return a.getKey() - b.getKey();
            });
            
            // 输出结果
            StringBuilder result = new StringBuilder();
            for (Map.Entry<Character, Integer> entry : list) {
                result.append(entry.getKey());
            }
            System.out.println(result.toString());
        }
    }
}
while True:
    try:
        s = input()
        
        # 统计每个字符出现次数
        char_count = {}
        for c in s:
            char_count[c] = char_count.get(c, 0) + 1
        
        # 按照次数和ASCII码排序
        sorted_chars = sorted(char_count.items(), 
                            key=lambda x: (-x[1], x[0]))
        
        # 输出结果
        result = ''.join(char for char, _ in sorted_chars)
        print(result)
        
    except:
        break

算法及复杂度

算法分析

  1. 统计过程:

    • 遍历字符串统计每个字符出现次数
    • 使用哈希表或数组存储统计结果
  2. 排序过程:

    • 自定义排序规则
    • 先按出现次数降序
    • 次数相同时按ASCII码升序

复杂度分析

  • 时间复杂度: O ( n log ⁡ n ) \mathcal{O}(n\log n) O(nlogn) - 其中 n n n 是不同字符的数量,主要是排序的复杂度
  • 空间复杂度: O ( k ) \mathcal{O}(k) O(k) - k k k 是字符集大小(这里是128,常数空间)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值