zoukankan      html  css  js  c++  java
  • 献上两个java小算法

    直接上代码:

      1 /**
      2  * Name: 求数组中元素重复次数对多的数和重复次数
      3  * Description: 
      4  * 数组中的元素可能会重复,这个方法可以找出重复次数最多的数,同时可以返回重复了多少次。
      5  * 但需要知道这个数组中最大的元素是多少,如果无法确定,就悲剧啦~
      6  *
      7  * @param array目标数组;
      8  *           max数组中数据的最大值;
      9  * @return 返回一个包含重复次数最多的数(value)和重复次数(maxCount)的map集合;
     10  *                  内部出现异常,默认返回0;
     11  * @throws 
     12  * @Author 杨元
     13  * Create Date: 2013-11-1 上午11:25:08
     14  */
     15 public static Map<String, Integer> arraySearch(int[] array,int max){
     16   //结果集合
     17   Map<String, Integer> resultMap = new HashMap<String, Integer>();
     18   //重复的次数
     19   int maxCount = 0;
     20   //重复次数对多的数
     21   int value = 0;
     22   
     23   try{
     24     //初始化数据数组,用来存放每个元素出现的次数
     25     int[] dataArray = new int[max+1];
     26     
     27     //遍历要查找的数组,以每个元素为下标,直接定位数据数组,进行+1操作,表示出现了一次
     28     for(int i : array){
     29       dataArray[i]++;
     30     }
     31     
     32     //找到数据数组中最大值
     33     for(int i=0;i<dataArray.length;i++){
     34       if(dataArray[i]>maxCount){
     35         maxCount=dataArray[i];
     36         value=i;
     37       }
     38     }
     39   }catch (Exception e) {}
     40   
     41   resultMap.put("maxCount", maxCount);
     42   resultMap.put("value", value);
     43   
     44   return resultMap;
     45 } 
     46 
     47 /**
     48  * Name: 比较两个字符串大小
     49  * Description: 比较的规则和数据库中的order by效果一致;
     50  *                 null自动转为空,空字符串最大;
     51  * 
     52  * @param first 要比较的第一个字符串;
     53  *           second 要比较的第二个字符串;
     54  * @return first大于second返回正数;
     55  *            first等于second返回0;
     56  *         first小于second返回负数;
     57  *         内部异常默认返回0;
     58  *         返回值非固定值哦~~;
     59  * @throws 
     60  * @Author 杨元
     61  * Create Date: 2013-11-2 上午10:10:04
     62  */
     63 public static int compareString(String first,String second){
     64   int result = 0;
     65   
     66   try{
     67     //null转空
     68     first = first==null?"":first;
     69     second = second==null?"":second;
     70     
     71     //预先记录字符串长度,避免反复读取
     72     int firstLength=first.length();
     73     int secondLength=second.length();
     74     
     75     //处理含有空串的特殊情况
     76     if("".equals(first) || "".equals(second)){
     77       //谁长谁小
     78       result = secondLength-firstLength;
     79     }else{
     80       //临时空间,用来存放ascii码总和
     81       int firstCount = 0;
     82       int secondCount = 0;
     83       //用纯运算得出两个数中较小的数,实在是bt
     84       int minLength = (secondLength*(firstLength/secondLength) + firstLength*(secondLength/firstLength))/(firstLength/secondLength + secondLength/firstLength);
     85       //按两个字符串中较短的位数去逐位截取,防止越界
     86       for(int i=0;i<minLength;i++){
     87         //求ascii码和
     88         firstCount+=first.substring(i,i+1).getBytes()[0];
     89         secondCount+=second.substring(i,i+1).getBytes()[0];
     90         //和不相等,说明已经比较出了大小
     91         if(firstCount!=secondCount){
     92           break;
     93         }
     94       }
     95       
     96       if(firstCount==secondCount){
     97         //长度长的大
     98         result = firstLength-secondLength;
     99       }else{
    100         //总和大的大
    101         result = firstCount-secondCount;
    102       }
    103     }
    104   }catch (Exception e) {}
    105   
    106   return result;
    107 }

             这是小菜利用闲暇时间所写。。

             小菜觉得这两个算法仅仅是实现了而已,效率应该不是很高,但还可以凑合用。。。也可以给像俺这样的菜鸟提供一个思路。。。

             代码中有详细的注释,小菜就不多说啦~

  • 相关阅读:
    java基础之 javac编译单个文件并执行 带jar包
    java 按照字符数分解字符串
    转载 模糊查询map中的key
    public final static PrintStream out = null; 的实例化猜想
    从0开始搭个网站在云上 思路引导
    java 泛型操作
    git 命令行
    React Native 安卓添加阴影
    react native 按钮的那些事
    mac 下 react Native android环境搭建
  • 原文地址:https://www.cnblogs.com/iyangyuan/p/3412800.html
Copyright © 2011-2022 走看看