网易 2022-3-27

第一题  dfs  过93.3%
package org.oj01;

import java.util.*;

public class Main {

    static int res=Integer.MAX_VALUE;
    static int nn;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);

        int a=sc.nextInt();int b=sc.nextInt();int x=sc.nextInt();int y=sc.nextInt();

        dfs(a,b,x,y,0);

        System.out.println(res);

    }

    public static void dfs(int a,int b,int x,int y,int cnt){
        if(a<=0&&b<=0){
            System.out.println("cnt"+cnt);
            res=Math.min(res,cnt);
        }
        else if(a>0&&b<=0){
            dfs(a-x,b,x,y,cnt+1);
        }
        else if(b>0&&a<=0){
            dfs(a,b-x,x,y,cnt+1);
        }
        else{dfs(a-y,b-y,x,y,cnt+1);}
    }

}

第二道dp
import java.util.*;

public class Main {

    static int res=Integer.MAX_VALUE;
    static int nn;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        int n=s.length();
        int[] dp=new int[n+1];
        dp[0]=0;
        if(s.charAt(0)==s.charAt(1)||Math.abs(s.charAt(1)-s.charAt(0))==1){
            dp[1]=s.charAt(0)-'a'+1+s.charAt(1)-'a'+1;
        }
        System.out.println(dp[1]);
        for(int i=2;i<n;i++){
            if(s.charAt(i)==s.charAt(i-1)||(s.charAt(i)-s.charAt(i-1)==1)){
                dp[i]=dp[i-2]+s.charAt(i)-'a'+1+s.charAt(i-1)-'a'+1;
            }else{
                dp[i]=dp[i-1];
            }
        }
        for(int i=0;i<n;i++){
            System.out.print("dp[i]"+dp[i]+" ");
        }
        System.out.println(dp[n-1]);
    }
}

第三道 0%
package org.oj01;

import java.util.*;

public class Main {

    static int res=Integer.MAX_VALUE;
    static int nn;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        nn=n;

        TreeNode root=build(n);
        System.out.println(root);
        //bfs
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if(node==null){
                continue;
            }
            System.out.print(node.val+" ");
            if(node.left!=null){
                queue.offer(node.left);
            }
            if(node.right!=null){
                queue.offer(node.right);
            }
        }

    }

    public static TreeNode build(int cur){
        if(cur<=0||cur>nn){
            return null;
        }
        TreeNode root;
        if(cur%2==0){
            root=new TreeNode(cur/2);
            root.left=build(cur/2-1);
            root.right=build(cur/2+1);
        }else{
            root=new TreeNode(cur/2+1);
            root.left=build(cur/2);
            root.right=build(cur/2+2);
        }
        return root;
    }

}
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int val){
        this.val=val;
    }
    public TreeNode(int val,TreeNode left,TreeNode right){
        this.val=val;
        this.left=left;
        this.right=right;
    }
}




第四道  回溯
package org.bd;

import java.util.Scanner;

public class Main {

    static int[] dx={0,1,0};
    static int[] dy={1,0,-1};
    static int res=Integer.MAX_VALUE;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();int m=sc.nextInt();
        int[][] matrix=new int[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                matrix[i][j]=sc.nextInt();
            }
        }
        boolean[][] vis=new boolean[n][m];

        dfs(matrix,0,0,n,m,0,vis);
        System.out.println(res);
    }
    public static void dfs(int[][] matrix,int start,int end,int n,int m,int cost,boolean[][] vis){
        if(start==n-1&&end==m-1){
            res=Math.min(res,cost);
        }
        if(cost>res){
            return;
        }
        vis[start][end]=true;
        for(int k=0;k<3;k++){
            int a=start+dx[k];
            int b=end+dy[k];
            if(a<0||a>=n||b<0||b>=m||vis[a][b]){
                continue;
            }
            if(matrix[start][end]==matrix[a][b]){
                cost+=1;
                vis[a][b]=true;
                dfs(matrix,a,b,n,m,cost,vis);
                vis[a][b]=false;
                cost-=1;
            }else{
                cost+=2;
                vis[a][b]=true;
                dfs(matrix,a,b,n,m,cost,vis);
                vis[a][b]=false;
                cost-=2;
            }

        }
    }
}



#春招##实习##笔试题目#
全部评论
第三题如果是树的那题,其实很简单,先把所有偶数输出,再把奇数输出就可以了...
点赞 回复 分享
发布于 2022-03-27 17:19

相关推荐

12-27 22:14
门头沟学院 Java
点赞 评论 收藏
分享
12-24 20:52
武汉大学 Java
点赞 评论 收藏
分享
是腾讯的csig腾讯云,前天晚上九点突然打电话约面,激动的通宵学了一晚上,第二天状态很差改了今天(以后再也不通宵学习了)感觉自己浪费了面试官一个半小时单纯手写+场景,无八股无项目无算法,打击真的很大,全是在面试官提醒的情况下完成的,自己技术方面真的还是有待提高,实力匹配不上大厂和已经面试的两个公司完全不一样,很注重编码能力和解决问题的能力,然而我这两个方面都很薄弱,面试官人很好很耐心的等我写完题目,遇到瓶颈也会提醒我,写不出题也会很耐心的跟我讲解好感动,到最后面试结束还安慰我打算把下周最后一场面试面完之后就不面啦,如果能去实习还是很开心,但是最重要的还是好好努力提高技术以下是面经第一题//&nbsp;实现一个解析&nbsp;url&nbsp;参数的函数function&nbsp;parseUrl(urlStr)&nbsp;{//&nbsp;TODO}parseUrl('*********************************************');//&nbsp;返回&nbsp;{a:&nbsp;1,&nbsp;b:&nbsp;2,&nbsp;c:&nbsp;3}追问:在链接里见过什么部分?用&nbsp;hash&nbsp;路由的话放在哪第二题//&nbsp;考虑有一个异步任务要执行,返回&nbsp;Promise,这个任务可能会失败,请实现&nbsp;retry&nbsp;方法,返回新方法,可以在失败后自动重试指定的次数。/***&nbsp;异步任务重试*&nbsp;@param&nbsp;task&nbsp;要执行的异步任务*&nbsp;@param&nbsp;times&nbsp;需要重试的次数,默认为&nbsp;3&nbsp;次*/function&nbsp;retry(task,&nbsp;times&nbsp;=&nbsp;3)&nbsp;{//&nbsp;TODO:&nbsp;请实现}//&nbsp;---------------测试示例&nbsp;----------------//&nbsp;原方法const&nbsp;request&nbsp;=&nbsp;async&nbsp;(data)&nbsp;=&gt;&nbsp;{//&nbsp;模拟失败if&nbsp;(Math.random()&nbsp;&lt;&nbsp;0.7)&nbsp;{throw&nbsp;new&nbsp;Error('request&nbsp;failed');}const&nbsp;res&nbsp;=&nbsp;await&nbsp;fetch(&#39;https://jsonplaceholder.typicode.com/posts&#39;,&nbsp;{method:&nbsp;'POST',body:&nbsp;JSON.stringify(data),});return&nbsp;res.json();}//&nbsp;新的方法const&nbsp;requestWithRetry&nbsp;=&nbsp;retry(request);//&nbsp;使用async&nbsp;function&nbsp;run()&nbsp;{const&nbsp;res&nbsp;=&nbsp;await&nbsp;requestWithRetry({&nbsp;body:&nbsp;'content'&nbsp;});console.log(res);}run();第三题就是给&nbsp;retry&nbsp;函数添加类型注释,用到泛型第四题:在组件库中将&nbsp;Alert&nbsp;用&nbsp;api&nbsp;的形式实现(应该就是&nbsp;message&nbsp;这个组件)怎么渲染到一个浮层里而不是原地渲染出来
不知道怎么取名字_:技术这个东西,太杂了,而且要下功夫的
查看5道真题和解析
点赞 评论 收藏
分享
评论
1
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务