以字符串中出现次数最多的字符的个数为权值,对一个字符串数组进行升序排序

介绍了一种新的字符串排序算法,该算法依据字符串中出现次数最多的字符的数量进行排序。通过自定义的getWeight方法计算字符串权重,并使用插入排序的方式实现整体排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class FindMostChar {

 /**
  * @param args
  */
 public static void main(String[] args) {
  sortByMostChar(args);

  for (int i = 0; i < args.length; ++i) {
   System.out.println(args[i]);
  }
 }

 public static void sortByMostChar(String[] strings) {

  for (int i = 1; i < strings.length; ++i) {
   String tmpString = strings[i];

   int j = i;
   for (; j > 0 && (getWeight(tmpString) < getWeight(strings[j - 1])); --j) {
    strings[j] = strings[j - 1];
   }
   strings[j] = tmpString;
  }
 }

 /*
  * 找出指定字符串中出现次数最多的字符的个数,具体算法为:将指定字符串中的每个字符逐个与所有字符相比较,如果相

  * 等,则计数器+1
  * 每轮比较完后,将计数器的值与当前字符个数最大值比较,如果计数器的值较大则将其值赋予字符个数最大值, 当字符串

  * 遍历完毕后,所得的字符个数最大值即为所求
  */
 private static int getWeight(String string) {
  int maxTimes = 0;
  boolean[] markChecked = new boolean[string.length()]; // 用于标记已测试过的字符,避免重复比较

  for (int i = 0; i < markChecked.length; ++i) {
   markChecked[i] = false;
  }

  for (int i = 0; i < string.length(); ++i) {
   char tmpChar = string.charAt(i);
   int tmpTimes = 0; // 计数器

   if (!markChecked[i]) {
    for (int j = 0; j < string.length(); ++j) { // 用当前字符与所有字符相比较,如果相等,则计数器+1
     if (tmpChar == string.charAt(j)) {
      ++tmpTimes;
      markChecked[j] = true;
     }
    }
   }

   if (tmpTimes > maxTimes) { // 如果计数器大于当前字符个数最大值
    maxTimes = tmpTimes;
   }
  }

  return maxTimes;
 }
}

 

<script> // 封装优先级队列 function Priorityqueue(){ // 在priorityqueue重新创建了一个类,可以理解为内部类 function queueelement(element,priority){ this.element=element this.priority=priority } // 封装属性,用于存储队列里面的元素 this.items=[] // 实现插入方法 Priorityqueue.prototype.enqueue=function(element,priority){ // 1.创建Queueelement对象 var Queueelement=new queueelement(element.priority) // 如果原先队列里面没有元素,那插入的时候会先进行比较,如果原先里面有元素,那插入的时候要进行优先级比较 // 2,判断队列是否为空 if(this.items.length==0){ // 如果为空,直接进行插入,不进行比较 this.items.push(Queueelement) } // 如果不为空先进行判断权值 // 如果队列中存在比他大的 else{ var added=false for(var i=0;i<this.items.length;i++){ // 进行一个权值判断 if(Queueelement.priority<this.items[i].priority){ this.items.splice(i,0,Queueelement) added=true break } } // 如果不存在比他大的,就直接插入到最后 if(!added){ this.items.push(Queueelement) } } } // 2.从队列中删除前端元素 Priorityqueue.prototype.dequeue = function () { // 删除队列前端的数据 return this.items.shift() } // 3.查看前端元素 Priorityqueue.prototype.front = function (element) { return this.items[0] } // 4.查看队列是否为空 Priorityqueue.prototype.isEmpty = function () { return this.items.length === 0 } // 5.查看队列中元素的个数 Priorityqueue.prototype.size = function () { return this.items.length } // 6.将队列元素转换成为字符串 Priorityqueue.prototype.toString = function () { var str='' for(var i=0;i<this.items.length;i++){ str+=this.items[i].element+'-'+this.items[i].priority+' ' } return str } } // 测试代码 var pq=new Priorityqueue() // 验证一下是否实现优先级队列的功能 pq.enqueue('a', 11) pq.enqueue('c', 33) pq.enqueue('b', 22) alert(pq) </script>
最新发布
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值