search

Home  >  Q&A  >  body text

java - 一个算法问题

有一个业务场景,会随机出现一个字母或数字(a到z或0到9),我需要将这个字母或数字平均映射到两个值上(比如:01和02),用Java该如何实现?最好有代码,谢谢

阿神阿神2799 days ago415

reply all(1)I'll reply

  • 阿神

    阿神2017-04-17 11:04:55

    I don’t understand what the poster meant. Is it fixed mapping or random mapping? Just make a table for fixed mapping

    Map<Charactor, String> mapping = new HashMap<Charactor, String>();
    mapping.put('a', "01");
    mapping.put('b', "02");
    mapping.put('c', "01");
    mapping.put('d', "02"); // 或者for循环打表也一样
    
    
    String map(char src) {
        return mapping.get(src); // 省略了范围判断
    }
    

    reply
    0
  • Cancelreply