华为2020秋招笔试题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/smarryw/article/details/98973837

一、全量字符集与已占用字符集


输入描述:

输入一个字符串,字符串中包含了全量字符集和已占用字符集,两个字符集用@相连。@前的字符集合为全量字符集,@后的字符集为已占用字符集合。已占用字符集中的字符一定是全量字符集中的字符。字符集中的字符跟字符之间使用英文逗号分隔。字符集中的字符表示为字符加数字,字符跟数字使用英文冒号分隔,比如a:1,表示1个a字符。字符只考虑英文字母,区分大小写,数字只考虑正整形,数量不超过100,如果一个字符都没被占用,@标识符仍在,例如a:3,b:5,c:2@

输出描述:

可用字符集。输出带回车换行。

示例1:

输入:a:3,b:5,c:2@a:1,b:2

输出:a:2,b:3,c:2

说明:全量字符集为3个a,5个b,2个c。已占用字符集为1个a,2个b。由于已占用字符集不能再使用,因此,剩余可用字符为2个a,3个b,2个c。因此输出a:2,b:3,c:2。注意,输出的字符顺序要跟输入一致。不能输出b:3,a:2,c:2。如果某个字符已全被占用,不需要输出。例如a:3,b:5,c:2@a:3,b:2,输出为b:3,c:2。
 

思路:

这道题主要涉及是对字符串的分割,拼接。。。

下面是我自己的代码,如有改进的地方,欢迎提出!

public class HuaWei_08_07 {

    public static void main(String[] args){
      String s="a:3,b:5,c:2@a:3,b:1";
      HuaWei_08_07 hua=new HuaWei_08_07();
      String useStr=hua.UserStrings(s);
      System.out.print(useStr);
    }

    public String UserStrings(String s){
          String[] str=s.split("@");
          if(str.length==1){
              return str[0];
          }

         Map<String,Integer> map=new HashMap<>();
          for(int i=0;i<str.length;i++){
              map=putmap( map,str[i].split(","),i);
          }
          
        StringBuffer buf=new StringBuffer();
        for(String key:map.keySet()){
            if(map.get(key)!=0) {
               buf.append(key+":"+map.get(key)+",");
            }
        }
        String s1=buf.toString();
        return s1.substring(0,s1.length()-1);

    }


    public Map<String,Integer> putmap(Map<String,Integer> map,String[] str,int flag){
        for(int i=0;i<str.length;i++){
            String s2=str[i].split(":")[0];
            int a=Integer.parseInt(str[i].split(":")[1]);
            if(flag==1&&map.containsKey(s2)){
                int b=map.get(s2)-a;
                map.put(s2,b);
            }else{
                map.put(s2,a);
            }
        }

        return map;
    }

}

二、Trie树


题目描述:


如下是一棵Trie树,圆圈表示内部节点,指向孩子节点的每个标记的值范围在0-255之间,每个内部节点最多有256个孩子节点。三角形表示叶子节点,每个叶子节点中存储一个value,根节点到叶子节点之间路径上的所有字符构成一个完整key。

输入描述:


第1行的数字M表示Labels、HasChild、POUDS数组大小,紧跟着的3行分别表示Labels、HasChild、POUDS数组内容,用空格分开。第5行的数字N表示Values数组大小,随后1行表示Values数组内容。第7行的数字表示Key数组大小,随后1行表示要查找的key字符数组。

输出描述:


输出一行key对应的value,若key不存在,输出0。

示例1:


输入:

15
115 112 116 97 111 121 114 101 105 112 121 114 102 115 116
0 0 0 1 1 0 1 0 0 0 0 1 1 1 1
1 1 0 1 0 1 1 1 0 0 0 1 1 0 0
8
1 2 3 4 5 6 7 8
3
116 114 112

输出:

7

三、逻辑计算


题目描述:


常用的逻辑计算有And(表示为&);Or(表示为|);Not(表示为!)。其中,他们的优先级关系是Not(!)>And(&)>Or(|)。

输入描述:


1、测试用例中间无空格,无需考虑空格。

2、测试用例表达式只会出现如下字符:“0”,“1”,“(”,“)”,“&”,“|”,“!”。

3、测试用例所给的输入都是合法输入,无需要考虑非法输入。

4、测试用例长度不会超过128个字符。

5、括号可以重复嵌套。

例如:

1 | ( 1 & 0 )                       返回值:1

1 & 0 | 0 & 1                     返回值:0

! 0 & 1 | 0                          返回值:1

( ( ! 0 & 1 ) ) | 0                 返回值:1

输出描述:


输出逻辑运算后的最终结果:0或者1

示例1:
输入:! ( 1 & 0 ) | 0 & 1

输出:1

示例2:
输入:! ( 1 & 0 ) & 0 | 0

输出:0
第一种解法:调用函数

 /**第一种 调用函数*/
    public static  void Pipei(String s){

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        Object result=null;
        try {
            result = engine.eval(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

第二种:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        int k = 0;
        while (input.length() != 1) {
            input = input.replaceAll("!1", "0");
            input = input.replaceAll("!0", "1");

            input = input.replaceAll("1&0", "0");
            input = input.replaceAll("0&1", "0");
            input = input.replaceAll("0&0", "0");
            input = input.replaceAll("1&1", "1");


            input = input.replaceAll("1\\|0", "1");
            input = input.replaceAll("0\\|1", "1");
            input = input.replaceAll("0\\|0", "0");
            input = input.replaceAll("1\\|1", "1");

            input = input.replaceAll("\\(1\\)", "1");
            input = input.replaceAll("\\(0\\)", "0");

            k++;
            if (k > 20) break;
        }
        System.out.println(input);
        sc.close();
    }

猜你喜欢

转载自blog.csdn.net/smarryw/article/details/98973837