华为机试9.1(通信/射频算法岗)

从7月份开始围观牛客大佬们的面经,收获很大。昨天考完机试,记录一下题目。

第一题:模糊搜索【不会&没时间做】
输入描述:
第一行是搜索词,第二行是目标文本,包含了多个目标词的一行文本。
输入的约束:1.搜索词和目标词是一个单词 2.单词定义:长度为[0,100]之间的字符串,仅有大写字母,小写字母,数字和中横线-组成。

第一题把样例中的答案输出一个能过5%

第2题:BFS求最短路径问题
题目描述:
小明从矩阵的左上角开始走到矩阵右下角所需要的最小的次数。
矩阵:有0和1组成,1是陆地(可以走路的)0是水面(不能走路的)
其他条件:(1)每次可以从上下左右四个方向走(2)小明每次走路喜欢1 2 1 2 1 2...交替走:即第一次走1步,第2次走2步,第3次走1步,第4次走2步...

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @Description
 * 思路:从起点到终点
 * 1.BFS
 * 2.加一个判断当前层数的变量count:
 *      若count为奇数:走一步
 *      若count为偶数,走两步
 */
public class Main {
    //走一步
    private static int[][] Direction1 = new int[][]{{-1,0},{0,1},{1,0},{0,-1}};
    //走两步
    private static int[][] Direction2 = new int[][]{{-2,0},{0,2},{2,0},{0,-2}};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int M = sc.nextInt();//写入矩阵的行
        int N = sc.nextInt();//写入矩阵的列
        int[][] nums = new int[M][N];
        for(int i=0;i<M;i++){
            for(int j=0;j<N;j++){
                nums[i][j] = sc.nextInt();//输入矩阵
            }
        }

        int res = solution(nums);
        System.out.println(res);


    }

    public static int solution(int[][] nums){
        int row = nums.length;
        int col = nums[0].length;

        boolean[][] visited = new boolean[row][col];
        Queue<int[]> queue = new LinkedList<>();//队列来记录矩阵的索引,有数组表示[i,j]
        queue.offer(new int[]{0,0});//队列的初始化,将起点(0,0)放进去
        int count=0;//记录层数

        while(!queue.isEmpty()){
            int size = queue.size();
            count ++;

            for(int i=0;i<size;i++){
                int[] tmp = queue.poll();

                //count为奇数时,走一步
                if(count % 2 != 0){
                    for(int[] edge : Direction1){
                        int newx = edge[0] + tmp[0];
                        int newy = edge[1] + tmp[1];

                        if(isValid(newx,newy,nums) && !visited[newx][newy] && nums[newx][newy] == 1){
                            if(newx == row-1 && newy == col-1){
                                return count;
                            }
                            visited[newx][newy] = true;
                            queue.offer(new int[]{newx,newy});
                        }
                    }
                }
                
                //count为奇数时,走一步
                if(count % 2 == 0){
                    for(int[] edge : Direction2){
                        int newx = edge[0] + tmp[0];
                        int newy = edge[1] + tmp[1];

                        if(isValid(newx,newy,nums) && !visited[newx][newy] && nums[newx][newy] == 1){
                            if(newx == row-1 && newy == col-1){
                                return count;
                            }
                            visited[newx][newy] = true;
                            queue.offer(new int[]{newx,newy});
                        }
                    }
                }
            }


        }

        return 0;


    }

    static boolean isValid(int i,int j,int[][] nums){
        int row = nums.length;
        int col = nums[0].length;

        if(i<0 || i>=row || j<0 || j>=col) return false;
        else return true;

    }
}
//这种写法过了75%,不知道哪里出了问题🤤
第三题:给了一大堆文字描述,提炼出来,就是求一个无向图有几个连通分量的问题,力扣上这种题很多。DFS就能100%
import java.util.*;

/**
 * @Description 求连通的个数
 * 输入描述:
 * 第一个数为顶点与顶点之间的连接关系数N
 * 接下来输入N行,表示两顶点之间相连
 *
 * 输出:有几个连通图
 *
 * 输入:
 * 6
 * [0 1]
 * [0 2]
 * [0 3]
 * [0 4]
 * [4 5]
 * [6 7]
 *
 * 输出:
 * 2
 * //不知道顶点个数,而且顶点是无序的,并非为0~n-1这样的,
 * 所以构造图时采用HashMap,同时记录顶点和对应的邻接表
 * @creat 2021-09-01
 */
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        HashMap<Integer, Set<Integer>> adj = new HashMap<>();
        HashMap<Integer, Boolean> visited = new HashMap<>();//标记顶点是否被访问
        //构建无向图
        //1.构建顶点:
        Set<Integer> set = new HashSet<>();//去重操作
        List<Integer> list = new LinkedList<>();//存放顶点的list
        int[][] edges = new int[N][2];//获取输入数据
        
        for(int i=0;i<N;i++){
            edges[i][0] = sc.nextInt();
            edges[i][1] = sc.nextInt();

            if(!set.contains(edges[i][0])){
                list.add(edges[i][0]);
                set.add(edges[i][0]);
            }
            if(!set.contains(edges[i][1])){
                list.add(edges[i][1]);
                set.add(edges[i][1]);
            }

        }

        //对每个顶点构建邻接表(初始化):
        for(Integer g : list){
            adj.put(g,new HashSet<>());
            visited.put(g,false);
        }

        //添加顶点间的指向
        for(int[] edge : edges){
            adj.get(edge[0]).add(edge[1]);
            adj.get(edge[1]).add(edge[0]);
        }


        //对每个顶点进行DFS遍历
        //访问当前节点标记为true

        int count=0;//记录连通图数量
        for(Integer i : list){
            if(!visited.get(i)){
                dfs(i,adj,visited);
                count++;
            }
        }

        System.out.println(count);
    }

    static void dfs(int i, HashMap<Integer, Set<Integer>> adj ,HashMap<Integer, Boolean> visited){
        visited.put(i,true);
        Set<Integer> temp = adj.get(i);//当前顶点的邻接表

        for(Integer g : temp){
            if(!visited.get(g)){
                dfs(g,adj,visited);
            }
        }

    }
}


PS:通信/射频算法机试题确实比软开的简单😅

#华为机试##华为##笔经#
全部评论
楼主,能说一下第二题的解题思路吗?我C++看不懂Java啊
1
送花
回复
分享
发布于 2021-09-13 11:09
华为云 校招、社招 大量招人!!HC充足!!工作地深圳/杭州/西安!!欢迎大家踊跃投递,可内推跟进,详细信息可私聊咨询,或者加微信18717376587了解(验证信息请参考:牛客+姓名+学校+学历+当前公司+当前工作岗位(应届生无需后两项))!!    华为云是华为的云服务品牌,将华为30多年在ICT领域的技术积累和产品解决方案开放给客户,致力于提供稳定可靠、安全可信、可持续创新的云服务,赋能应用、使能数据、做智能世界的“黑土地”,推进实现“用得起、用得好、用得放心”的普惠AI。     在信息产业领域,芯片、操作系统、数据库是技术门槛最高的3个核心产业控制点,数据库也被誉为软件皇冠。我们负责数据库云服务产品定义、开发、运维、运营,通过运维运营驱动研发,构建数据库云服务竞争力(含运维运营能力、功能、性能、质量、成本等要素),提升客户满意度,构筑业界领先的产品和竞争力,对商业成功负责。
点赞
送花
回复
分享
发布于 2021-09-02 11:23
滴滴
校招火热招聘中
官网直投
通信算法就是算法工程师下面的通信算法吧?请问就是三道编程题吗
点赞
送花
回复
分享
发布于 2021-09-02 21:55
楼主 看您的标题 通信算法和射频算法同一套题吗  还是在笔试的时候 试卷那么显示呢
点赞
送花
回复
分享
发布于 2021-09-03 10:49
羡慕 我跟你一模一样的题 第一题过了70% 其他的没做 挂了😅
点赞
送花
回复
分享
发布于 2021-09-05 18:19
楼主,可以问一下你现在有这三个题的答案嘛
点赞
送花
回复
分享
发布于 2021-09-10 21:28
第三题直接并查集造轮子代码应该简单一些😁
点赞
送花
回复
分享
发布于 2021-09-11 18:26
您好,我想问一下,通信算法岗的机试题和软开的机试题一样不?
点赞
送花
回复
分享
发布于 2022-08-22 22:32 陕西

相关推荐

5 52 评论
分享
牛客网
牛客企业服务