Qunar大连开发笔试第3题解法

1.
  1. public class WordEntity implements Comparable {
  2.     private Integer count = null;

  3.     public String getWord() {
  4.         return word;
  5.     }

  6.     public void setWord(String word) {
  7.         this.word = word;
  8.     }

  9.     public Integer getCount() {
  10.         return count;
  11.     }

  12.     public void setCount(Integer count) {
  13.         this.count = count;
  14.     }

  15.     private String word = null;

  16.     public WordEntity(String word, Integer count) {
  17.         this.count = count;
  18.         this.word = word;
  19.     }

  20.     @Override
  21.     public int compareTo(WordEntity o) {
  22.         int cmp = count.intValue() - o.count.intValue();
  23.         return (cmp == 0 ? word.compareTo(o.word) : -cmp);
  24.     }
  25. }
2.
  1. import sun.reflect.generics.tree.Tree;

  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Set;
  5. import java.util.TreeSet;

  6. public class CountString {

  7.     public static TreeSet set = new TreeSet();

  8.     public static TreeSet outputStrs(String[] args) {

  9.         int count = 0;
  10.         for (int i = 0; i < args.length; i++) {
  11.             String temp = args[i].replaceAll("your", "");
  12.             count = (args[i].length() - temp.length()) / "your".length();
  13.             if (count > 0) {
  14.                 set.add(new WordEntity(args[i], count));
  15.             }
  16.         }

  17.         return set;
  18.     }

  19.     public static void main(String[] args) {
  20.         String[] strings = {"your", "assfyour", "dsfdfyouryour"};

  21.         TreeSet hi = outputStrs(strings);
  22.         for (Iterator he = hi.iterator(); he.hasNext(); ) {
  23.             WordEntity w = he.next();
  24.             System.out.println(w.getWord());
  25.         }
  26.     }

  27. }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值