华为OD机考题(基础API)

基础API

字典排序

复制代码
List<String> list=new ArrayList();
//add list member
Arrays.sort(list);

List<Map<String,Integer>> list=new ArrayList();
//add list member
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
   @Override
   public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2{
        return o2.getValue()-o1.getValue();
   }
});

进制转换

Integer.parseInt(hexString, 16)//16进制转10进制

Integer.toBinaryString(num);//十进制转二进制

int decimal = Integer.parseInt(binaryString, 2);//二进制转10进制

>> 有符号右移

>>>无符号右移

bit处理​​​​​​

​Integer.bitCount(n);//二进制中1的个数

字典排序示例:

复制代码
package huaweiod;

import java.util.*;

public class Test{
    public static void main(String[] args) throws Exception{
        Map<String,Map<String,Integer>> map=new HashMap();
        Map<String,Integer> map1=new HashMap();
        map1.put("004",160);
        map1.put("003",150);
        map1.put("002",150);
        map1.put("001",160);
        map1.put("005",150);
        map.put("02",map1);

        Map<String,Integer> map2=new HashMap();
        map2.put("003",130);
        map2.put("004",140);
        map.put("01",map2);

        Set<String> classNum=map.keySet();
        Object[] array=classNum.toArray();
        Arrays.sort(classNum.toArray());
        for(Object o:array){
            System.out.println("class:"+o);
            HashMap<String,Integer> tempMap=(HashMap)map.get(o);
            List<Map.Entry<String,Integer>> list=new ArrayList(tempMap.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                    return o2.getValue()-o1.getValue();
                }
            });
            for(Map.Entry<String,Integer> entry:list){
                System.out.println(entry.getKey()+":"+entry.getValue());
            }
        }
    }
}
相关推荐
塔中妖6 天前
【华为OD】分割数组的最大差值
数据结构·算法·华为od
塔中妖6 天前
【华为OD】数字游戏
算法·游戏·华为od
熊文豪7 天前
【华为OD】找出通过车辆最多颜色
算法·华为od
塔中妖7 天前
【华为OD】环中最长子串2
算法·华为od
熊文豪8 天前
【华为OD】区块链文件转储系统
算法·华为od·区块链
塔中妖8 天前
【华为OD】Linux发行版的数量
linux·算法·华为od
熊文豪8 天前
【华为OD】阿里巴巴找黄金宝箱
算法·华为od
塔中妖8 天前
【华为OD】5G网络建设
网络·5g·华为od
塔中妖8 天前
【华为OD】查找接口成功率最优时间段
算法·链表·华为od
塔中妖8 天前
【华为OD】最大子矩阵和
算法·华为od·矩阵