华为实习生笔试题目&题解

  1. 岗位:大数据开发工程师
  2. 笔试时间:2019年03月20日
import java.util.Scanner;
/**
 * 笔试第一题:输入一个字符串,表示100以内的加减法,计算结果
 * 实例 输入:“12 3-5” ,输出:10
 * @author yangkuan
 */
public class Main1 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        System.out.println(computeResult(s));
    }

    public static int computeResult(String s) {

        String[] splits = s.split("\\ |-");
        boolean[] flag = new boolean[splits.length - 1];
        int k = 0;
        for (int i = 0; i < s.length(); i  ) {
            if (s.charAt(i) == ' ') {
                flag[k  ] = true;
            } else if (s.charAt(i) == '-') {
                flag[k  ] = false;
            }
        }
        int result = Integer.valueOf(splits[0]);
        for (int i = 1; i < splits.length; i  ) {
            if (flag[i - 1] == true) {
                result  = Integer.valueOf(splits[i]);
            } else {
                result -= Integer.valueOf(splits[i]);
            }
        }
        return result;
    }
}

package com.second.huawei;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * 笔试第二题:寻找蛇形字符串
 * 使用hashmap分别存储大写字符和小写字符
 * @author yangkuan
 */
public class Main2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        List<String> results = findPythonString(s);
        if(results.size()==0){
            System.out.println("Not Found");
        }else {
            for(String result:results){
                System.out.println(result);
            }
        }
    }

    public static List<String> findPythonString(String s){
        HashMap<Character,Integer> lowMap = new HashMap<>();
        HashMap<Character,Integer> bigMap = new HashMap<>();
        for(int i=0;i<s.length();i  ){
            char ch = s.charAt(i);
            if(ch<='z'&&ch>='a'){
                lowMap.put(ch,lowMap.getOrDefault(ch,0) 1);
            }
            else if(ch<='Z'&&ch>='A'){
                bigMap.put(ch,bigMap.getOrDefault(ch,0) 1);
            }
        }
        List<String> list = new ArrayList<>();
        while (!bigMap.isEmpty()){
            for(char ch='A';ch<='Z';ch  ){
                if(bigMap.containsKey(ch)&&lowMap.containsKey(Character.toLowerCase(ch))){
                    StringBuilder sb = new StringBuilder();
                    sb.append(ch);
                    sb.append(Character.toLowerCase(ch));
                    bigMap.put(ch,bigMap.get(ch)-1);
                    lowMap.put(Character.toLowerCase(ch),lowMap.get(Character.toLowerCase(ch))-1);
                    if(bigMap.get(ch)==0) bigMap.remove(ch);
                    if(lowMap.get(Character.toLowerCase(ch))==0) lowMap.remove(Character.toLowerCase(ch));
                    goAhead(sb,lowMap,bigMap);
                    list.add(sb.toString());
                    break;
                }
                else if(bigMap.containsKey(ch)){
                    bigMap.remove(ch);
                }
                else if(lowMap.containsKey(Character.toLowerCase(ch))){
                    lowMap.remove(Character.toLowerCase(ch));
                }

            }
        }
        return list;
    }
    private static void goAhead(StringBuilder sb
            ,HashMap<Character,Integer> lowMap
            ,HashMap<Character,Integer> bigMap){
        char slowCh = (char) (sb.charAt(sb.length()-1) 1);
        char bigCh = Character.toUpperCase(slowCh);
        if(bigMap.containsKey(bigCh)&&lowMap.containsKey(slowCh)){
            sb.append(bigCh);
            sb.append(slowCh);
            bigMap.put(bigCh,bigMap.get(bigCh)-1);
            lowMap.put(slowCh,lowMap.get(slowCh)-1);
            if(bigMap.get(bigCh)==0) bigMap.remove(bigCh);
            if(lowMap.get(slowCh)==0) lowMap.remove(slowCh);
            goAhead(sb,lowMap,bigMap);
        }
        else if(bigMap.containsKey(bigCh)){
            bigMap.remove(bigCh);
        }
        else if(lowMap.containsKey(slowCh)){
            lowMap.remove(slowCh);
        }
    }
}
import java.util.*;

/**
 * 笔试第三题:状态机
 * 状态机首先定义状态,根据当前状态得到下一个状态
 * @author yangkuan
 */
public class Main3 {

    class Node {
        String preState;
        String eventSource;
        String object;
        String event;
        String nextState;

        public Node(String preState, String eventSource, String object, String event, String nextState) {
            this.preState = preState;
            this.eventSource = eventSource;
            this.object = object;
            this.event = event;
            this.nextState = nextState;
        }

        public String matchNode(String preState, String eventSource, String event) {
            if ((this.preState.equals(preState) || (this.preState.equals("no finished") && !preState.equals("finished")))
                    && this.eventSource.equals(eventSource)
                    && this.event.equals(event)) {
                return this.nextState;
            }
            return "false";
        }
    }

    Node node1 = new Node("init", "RmApp", "o", "start", "submitted");
    Node node2 = new Node("submitted", "ResourceScheduler", "o", "app_accepted", "scheduled");
    Node node3 = new Node("scheduled", "RmContainer", "o", "container_allocated", "allocated");
    Node node4 = new Node("scheduled", "ApplicationMasterLauncher", "o", "launched", "running");
    Node node5 = new Node("running", "ResourceScheduler", "o", "finished", "finished");
    Node node6 = new Node("no finished", "RmApp", "o", "kill", "killed");
    Node[] nodes = {node1, node2, node3, node4, node5, node6};


    public void transformState(String s) {
        String[] transforms = s.split(" ");
        Map<String,String> objectMap = new HashMap<>();
        for(String transform:transforms){
            String object =transform.split("\\|")[1];
            objectMap.put(object,"init");
        }
        for (String transform : transforms) {
            String[] strings = transform.split("\\|");
            String eventSource = strings[0];
            String object = strings[1];
            String event = strings[2];
            String nextState;
            for (Node node:nodes){
                if (!(nextState = node.matchNode(objectMap.get(object), eventSource, event)).equals("false")) {
                    System.out.println(object   "|"   nextState);
                    objectMap.put(object,new String(nextState));
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        new Main3().transformState(s);
    }
}
//  输入: RmApp|RmAppAttempt_001|start RmApp|RmAppAttempt_002|start RmApp|RmAppAttempt_001|kill
#实习##笔试题目##笔经##华为##题解#
全部评论
稳呀,都做出来了
点赞 回复
分享
发布于 2019-03-20 23:20
大佬啊
点赞 回复
分享
发布于 2019-03-20 21:48
淘天集团
校招火热招聘中
官网直投
倒数第三题过了吗?
点赞 回复
分享
发布于 2019-03-20 21:51
大佬你好,我想知道对于不同语言会有歧视吗,看实现过程吗?。比如Python要是写的话我直接写eval("xxx"),是不是得复杂化按字符串处理啊?
点赞 回复
分享
发布于 2019-03-20 21:54
楼主~请问有第二题的题目吗?(不太知道什么是蛇形字符串和具体的要求)
点赞 回复
分享
发布于 2019-03-20 22:51
第二题我用了两个长度26的数组,第三题自定义了一个类。最后都只过了80%😂
点赞 回复
分享
发布于 2019-03-20 23:41
有大佬neng讲一下其他的题目吗 目前还没有收到笔试通知,想了解下难度,能记住一道题也行
点赞 回复
分享
发布于 2019-03-21 08:52
大佬 能贴上完整的题目吗  感谢  (ps:第一题是不是用Integer.parseInt(s))
点赞 回复
分享
发布于 2019-03-21 11:12
华为笔试多久啊,一个半小时还是两个小时
点赞 回复
分享
发布于 2019-03-23 22:19
第一题输入应该时nextLine()吧😂😂
点赞 回复
分享
发布于 2019-03-31 14:35
请问,有选择,填空之类的嘛?
点赞 回复
分享
发布于 2019-04-03 10:49
第三道题目不能用Python,我这不gg了吗
点赞 回复
分享
发布于 2019-04-03 18:19

相关推荐

点赞 73 评论
分享
牛客网
牛客企业服务