牛客-真题练习-01

牛客-真题练习-01

58同城2020校园招聘笔试-后端

第一题

image-20220222123327338image-20220222123415027

第一题代码实现

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        String str = sc.nextLine();
        String[] strList = str.split(",");
        int count = 1;
        
        for(int i = 1;i < strList.length;i++){
            if(!strList[i].equals(strList[i-1])){
                count++;
            }
        }
        
        System.out.print(count);
    }
}

第二题

image-20220222123603359

image-20220222123631119

image-20220222123651847

第二题代码实现

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] list = new int[m][n];
        
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                list[i][j] = sc.nextInt();
            }
        }
        
        int[][] dp = new int[m][n];
        
        dp[0][0] = list[0][0];
        for(int i = 1;i < n;i++){
            dp[0][i] = dp[0][i-1] + list[0][i];
        }
        for(int i = 1;i < m;i++){
            dp[i][0] = dp[i-1][0] + list[i][0];
        }
        
        for(int i = 1;i < m;i++){
            for(int j = 1;j < n;j++){
                dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1]) + list[i][j];
            }
        }
        
        //for(int i = 0;i < m;i++){
        //    for(int j = 0;j < n;j++){
        //        System.out.print(dp[i][j]);
        //   }
        //   System.out.println();
        //}
        
        System.out.print(dp[m-1][n-1]);
    }
}

第三题

image-20220222123812163

image-20220222123848454

image-20220222123920115

第三题代码实现

import java.util.*;

public class Main{
    public static void main(String[] args){
         Scanner sc = new Scanner(System.in);
        
        int m = sc.nextInt();
        int[] list = new int[m];
        
        // 填充孩子的评分
        for(int i = 0;i < m;i++){
            list[i] = sc.nextInt();
        }
        
        // 造一个tx代表每个孩子应该分到的最小饼干。
        int[] childs = new int[m];
        int minSum = 0;
        childs[0] = 1;
        
        // 贪心,先确定左边比较的评分,再确定右边比较的评分,左边评分需要比较他右边的比较后的情况。
        for(int i = 1;i < m;i++){
            if(list[i] > list[i-1]){
                childs[i] = childs[i-1] + 1;
            } else{
                childs[i] = 1;
            }
        }
        
        for(int i = m-2;i >= 0;i--){
            if(list[i] > list[i+1]){
                childs[i] = Math.max(childs[i],childs[i+1] + 1);
            }
        }
        
        for(int i = 0;i < m;i++){
            minSum += childs[i];
        }
        
        System.out.print(minSum);
    }
}

总结

58的是ACM模式,需要自己导入库,而且需要自己写输入,做惯了力扣有些不适应。然后题目的话和力扣练过的题是很相似,感觉都做过,运气也是比较好可能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人山人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值