【问题标题】:Sorting frequency of chars字符的排序频率
【发布时间】:2014-02-14 23:53:40
【问题描述】:

我刚刚做了一个算法来计算字符串中字符的频率。我感到困惑的是如何对频率进行排序,以便将出现次数最多的字符列在顶部,而将出现次数最少的字符列在底部。

起初我尝试让另一个变量“fc”(用于频率计数器)与我原来的计数器变量“k”一致。但是我陷入了如何对这个频率进行排序的思考过程中,我制作的 fc var 毫无用处。

感谢您提供的任何帮助!

这是我的代码:

  import java.io.*;
public class Freq
{
    public static void main(String args[])throws IOException
    {
        //read input stream
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        int ci,i,j,k,l,fc;l=0;
        String str,str1;
        char c,ch;
        System.out.println("Enter your String");
        str=in.readLine();
        i=str.length();
        //cycle through ASCII table chars and obtain chars typed
        for(c='A';c<='z';c++)
        {
            k=0;
            fc=0;           //fc keeps count like k
            for(j=0;j<i;j++)
            {
                ch=str.charAt(j);
                if(ch==c)
                    k++;
                    fc=k-1;     //was going to represent this counter for 'less than k'

            }
            if(k>0)
            System.out.println("The character "+c+" has occured for "+k+" times");
        }
    }
}

【问题讨论】:

  • 首先我想说的是使用从 A 到 z 的循环。为什么你不只检查你的字符串中是否存在字符。意思是字符串“My Personal str”检查频率。仅针对这几个字符。然后对它们进行排序..我只能给你代码。不适用于所有字符 A 到 z

标签: java sorting frequency frequency-distribution


【解决方案1】:

这个比Hashmap解决方案更快:

    public static void frequencySort(String s) {
        int[] f = new int[256];
        for (int c : s.toCharArray())
            f[c]++;
        List<CharStore> list = new ArrayList<>();
        for (int i = 0; i < f.length; i++) {
            if (f[i] != 0) list.add(new CharStore(i, f[i]));
        }
        Collections.sort(list);
        for (CharStore c : list) {
            System.out.println(((char)c.c) + " has occured " + c.count + " times";  
        }
    }

    static class CharStore implements Comparable<CharStore> {
        int c;
        int count;

        public CharStore(int c, int count) {
            this.c = c;
            this.count = count;
        }

        @Override
        public int compareTo(CharStore o) {
            return o.count - count;
        }
    }

【讨论】:

    【解决方案2】:

    我会这样做:

    int[] frequencyArray =  new int['z' -'A'];
    
    String inputString = "ttttttttttttttttest";
    
    for(int i = 0; i<inputString.length();i++)
    {
        frequencyArray[inputString.charAt(i) -'A']++;
    }
    

    然后,您可以通过您选择的任何流行排序算法对该数组进行排序。

    EDIT使代码更节省内存。

    【讨论】:

      【解决方案3】:

      您需要先将它们全部存储起来。您可以使用 HashMap 来存储它们,它还将简化您的计数程序。然后 Collections.sort 对条目集进行排序。您需要创建一个 Comparable> 来比较条目值以进行排序。

      编辑添加示例代码....

          BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
          System.out.println("Enter your String");
          String line = in.readLine();
          HashMap<Character,Integer> counts = new HashMap<>();
          for(char c : line.toCharArray()) {
              Integer count = counts.get(c);
              if (count == null) {
                  count = 0;
              }
              counts.put(c, ++count);
          }
          List<Entry<Character,Integer>> list = new ArrayList<>(counts.entrySet());
          Collections.sort(list, new Comparator<Entry<Character,Integer>>() {
              @Override
              public int compare(Entry<Character, Integer> o1,
                      Entry<Character, Integer> o2) {
                  return o2.getValue() - o1.getValue();
              }
          });
          for(Entry<Character,Integer> entry : list) {
              System.out.println("The character "+entry.getKey() +" has occured for "+ entry.getValue()+" times");
          }
      

      【讨论】:

      • 哇,非常感谢!我只是在学习哈希图,所以这段代码肯定给了我很大的帮助!非常感谢!
      【解决方案4】:
      You can follow these steps:
      
      1) Create a class call CharCount having two fields : char and freq. Override equals to return true if characters are equal and override hashcode to return character's hashcode. Make it implement Comparable and override compare and return -1,0 or 1 based on values of freq of objects being compared
      2) Have a Set of CharCount
      3)Each time you find a character create an instance of this class with character and freq as 0.
      4)Check if it exists in set and update feq accordingly
      5) Sort set data yourself or call Collections.sort
      

      【讨论】:

      • 这也是一个很好的答案。除了使用自定义类而不是 Entry 和 Set 而不是 HashMap 之外,它与接受的几乎相同。
      【解决方案5】:

      创建一个函数计数,为您提供特定字符的计数并代表计数进行排序,例如

      if( count(str,str.charAt[j]) > count(str,str.charAt[j+1]) )
      SWAP
      

      在此之前最好将str转换为char数组,那么它会像

      count(chararr,charrarr[j]) 
      

      【讨论】:

        猜你喜欢
        • 2020-02-06
        • 1970-01-01
        • 2020-12-13
        • 1970-01-01
        • 1970-01-01
        • 2014-01-10
        • 1970-01-01
        • 1970-01-01
        • 2017-11-08
        相关资源
        最近更新 更多