【问题标题】:Sort by number of apearances按出场次数排序
【发布时间】:2020-02-20 15:20:06
【问题描述】:

例如,给定一个单词,我必须按该单词中出现的次数对其字母进行排序,如果 2 个字母出现相同的次数,它将按词典最小值排序。 现在,我已经开始查看一个字母在一个单词中出现了多少次,但从这里我不知道该怎么做。 该问题需要我使用 BufferedReader 和 BufferedWriter。

import java.util.*;

public class Main {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Map<Character, Integer> m = new HashMap<>();
    String s = sc.nextLine();
    for (int i = 0; i < s.length(); ++i) {
        char c = s.charAt(i);
        if (m.containsKey(c))
            m.put(c, m.get(c) + 1);
        else
            m.put(c, 1);
    }
    for (char letter = 'a'; letter <= 'z'; ++letter)
        if (m.containsKey(letter))
            System.out.println(letter + ": " + m.get(letter));
}

目前我正在发布单词中最常出现的字母,但我不知道如何按出现次数对它们进行排序,以防有两个字母出现的次数相同且最少字典序。

【问题讨论】:

  • 真的有理由使用缓冲写入器吗?
  • 在问题中添加一些示例,即输入 -> 输出。这样你就可以更好地澄清问题。
  • 相关/相同的课程/问题:Sort the letters of a word(同一个人:STRKLok?相同的代码???)

标签: java bufferedreader bufferedwriter


【解决方案1】:

我希望这是你想要的

public static void main(String[] args) {
    Map<Character, Integer> m = new HashMap<>();
    String testString = "Instructions";
    Map<Character, List<Character>> map = new HashMap<>();

    for (int i = 0; i < testString.length(); i++) {
        char someChar = testString.charAt(i);
        if (someChar == ' ') {
            continue;
        }
        char ch = testString.charAt(i);
        List<Character> characters = map.getOrDefault(Character.toLowerCase(ch), new ArrayList<>());
        characters.add(ch);
        map.put(Character.toLowerCase(ch), characters);
    }
    List<Map.Entry<Character, List<Character>>> list = new ArrayList<>(map.entrySet());

    list.sort((o1, o2) -> {
        if (o1.getValue().size() == o2.getValue().size()) {
            return o1.getKey() - o2.getKey();/// your lexicographic comparing
        }
        return o2.getValue().size() - o1.getValue().size();
    });

    list.forEach(entry -> entry.getValue().forEach(System.out::print));
}

【讨论】:

  • 没关系,但在问题结束时有一个问题可以帮助我。例如,我有输入:说明;作为输出,它必须向我展示:iinnttcuoss。也就是说,如果两个字母出现相同的次数,则字母按频率和字母顺序排序。
  • 为你修改)
  • "prog.java:23: error: ')' expected list.sort((o1, o2) -> {", 好的,当我想测试问题时,我得到了这个错误,可能来自 Java 版本还是想法?
  • 使用 sdk 8 或将 lambda 替换为匿名类
【解决方案2】:

要计算单词中的字母,您可以使用更简单的方法:

定义包含 26 个零的数组,扫描输入行并在该数组中增加适当的索引,因此如果您遇到 'a'(或 'A' - 这是相同的字母,但不同的符号) - 您将增加索引处的值0、b - 索引 1 等

在此扫描期间,您还可以计算出现次数最多的符号,如下所示:

public static void main(final String[] args) throws IOException {
    char maxSymbol = 0;
    int maxCount = 0;

    final int[] counts = new int[26]; // number of times each letter (a-z) appears in string
    try (final BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
        final String s = br.readLine().toLowerCase(); // calculate case-insensitive counts
        for (final char c : s.toCharArray()) {
            final int idx = c - 'a'; // convert form ASCII code to 0-based index
            counts[idx]++;

            if (counts[idx] > maxCount) {
                maxSymbol = c; // we found most occurred symbol for the current moment
                maxCount = counts[idx];
            } else if (counts[idx] == maxCount) { // we found 2nd most occurred symbol for the current moment, need to check which one is minimal in lexicographical order
                if (c < maxSymbol) {
                    maxSymbol = c;
                }
            }
        }
    }

    if (maxSymbol > 0) {
        System.out.println("Most frequent symbol " + maxSymbol + " occurred " + maxCount);
    }
}

我已经使用缓冲读取器从标准输入获取数据,但我不知道将缓冲写入器放在哪里,也许是为了打印结果?

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 2014-12-12
    相关资源
    最近更新 更多