华为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());
            }
        }
    }
}
相关推荐
行十万里人生5 天前
仓颉项目调试配置与多文件场景下的问题解析
开发语言·人工智能·机器学习·华为od·华为·华为云·harmonyos
蜗牛的旷野6 天前
华为OD机试_2025 B卷_小华地图寻宝(Python,100分)(附详细解题思路)
开发语言·python·华为od
蜗牛的旷野13 天前
华为OD机试_2025 B卷_返回矩阵中非1的元素个数(Python,100分)(附详细解题思路)
python·华为od·矩阵
CodeClimb19 天前
【华为OD-B卷-打印文件 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
哪 吒20 天前
2025B卷 - 华为OD机试七日集训第2期 - 按算法分类,由易到难,循序渐进,玩转OD(Python/JS/C/C++)
python·算法·华为od·华为od机试·2025b卷
什码情况1 个月前
星际篮球争霸赛/MVP争夺战 - 华为OD机试真题(A卷、Java题解)
java·数据结构·算法·华为od·面试·机试
python算法(魔法师版)1 个月前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
懒懒小徐1 个月前
2023华为od统一考试B卷【二叉树中序遍历】
数据结构·算法·华为od
纪元A梦1 个月前
华为OD机试真题——荒岛求生(2025A卷:200分)Java/python/JavaScript/C/C++/GO最佳实现
java·c语言·javascript·c++·python·华为od·go
懒懒小徐1 个月前
2023华为od机试C卷【跳格子3】
java·华为od·动态规划