华为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());
            }
        }
    }
}
相关推荐
sprite_雪碧2 天前
考研机试笔记-1输入输出
笔记·考研·华为od
无限码力6 天前
华为OD机试真题2026双机位C卷 C++实现【日志解析】
c++·华为od·华为od机试真题·华为od机考真题·华为od机试真题-日志解析
uesowys9 天前
华为OD算法开发指导-数据结构-图
数据结构·算法·华为od
快敲啊死鬼12 天前
机试day5
算法·华为od·华为
LqKKsNUdXlA1 个月前
Comsol冻土水热力耦合模型代做 可复现白青波,秦晓同模型 建立了路基水热耦合计算控制方程
华为od
开开心心_Every1 个月前
剪切板工具存500条,可搜索备份导入导出
linux·运维·服务器·华为od·edge·pdf·华为云
开开心心_Every1 个月前
在线看报软件, 22家知名报刊免费看
linux·运维·服务器·华为od·edge·pdf·华为云
uesowys1 个月前
华为OD算法开发指导-二级索引-Read and Write Path Different Version
java·算法·华为od
西电研梦2 个月前
26西电考研 | 寒假开始,机试 or C语言程序设计怎么准备?
c语言·考研·华为od·研究生·西安电子科技大学·计算机408
无限码力2 个月前
华为OD技术面真题 - Mysql相关 - 4
mysql·华为od·华为od技术面真题·华为od技术面八股·华为od技术面八股文·华为od技术面mysql相关