zoukankan      html  css  js  c++  java
  • 58同城笔试题:数组去重;分饼干(分糖果);最小路径和(leetcode64)

    1. 数组去重

    题目描述

    /**
     * 有序数组去重
     * 输出最终的数字个数
     * 输入:1,2,2
     * 输出:2
     * @author Turing
     *
     */

    代码

    import java.util.*;
    public class E {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String[] str = sc.nextLine().split(",");
            int len = str.length;
            int [] array = new int[len]; 
            for (int i = 0; i < len; i++) {
                array[i] = Integer.valueOf(str[i]);
            }
            int index = 1;
            for (int i = 0; i < len-1; i++) {
                if(array[i]!=array[i+1]){
                    array[index] = array[i+1];
                    index++;
                }
            }
            System.out.println(index);
        }
    }

    2. 分饼干(分糖果)

    题目描述

    /**
     * 分糖果问题类似
     * 分饼干问题,每个孩子至少一个饼干,
     * 如果两个孩子坐一起,评分高的孩子必须得到更多的饼干(左右都比较)
     * 输出老师购买饼干总数的最小值
     * 输入:
        6
        3
        6
        3
        5
        6
        2
     * 输出:
        10
     * 说明:第一个数表示孩子数为6;1+2+1+2+3+1=10
     */

    代码

    import java.util.*;
    public class E4 {
        public static int m;
        public static int n;
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            n = sc.nextInt();
            int[] score = new int[n];
            int[] count = new int[n];
            for (int i = 0; i < n; i++) {
                score[i] = sc.nextInt();
                
            }
            Arrays.fill(count, 1);
            for(int i=1; i<n; i++){
                if (score[i]>score[i-1]) {
                    count[i]=count[i-1]+1;
                 }
            }
            for(int i=n-2; i>=0; i--){
               if (score[i]>score[i+1] && count[i]<=count[i+1]) {
                   count[i]=Math.max(count[i], count[i+1]+1);
               }
            }
            int sum = 0;
            
            for (int i = 0; i < count.length; i++) {
                sum += count[i];
            }
            System.out.println(sum);
            
        }
    }

    3. 最小路径和(leetcode64)

    题目描述

    /**
     * 有一个地图,围棋棋盘,两点之间有行走距离起点为左上角,终点为右下角在地图上,
     * 每次行走只能沿线移动到移动的临近的点
     * 并累加路径计算一个人从地图的起点走到终点的最小路径为多少?
     * 输入:
     * m*n地图表示如下:
     * 3
     * 3
     * 1 3 4
     * 2 1 2 
     * 4 3 1
     * 其中 m=3,n=3表示3*3矩阵
     * 行走路径为: 下》右》右》下
     * 路径总长:1+2+1+2+1=7
     * 
     * @author Turing
     *
     */

    代码

    
    
    import java.util.*;
    public class E4 {
        public static int m;
        public static int n;
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            m = sc.nextInt();
            n = sc.nextInt();
            int [][] grid = new int[m][n]; 
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    grid[i][j] = sc.nextInt();
                }
            } 
            System.out.println(minpath(grid, 0, 0));
        }
        public static int minpath(int[][]grid, int i,int j){
            if(i==m||j==n){
                return Integer.MAX_VALUE;
            }
            if(i==m-1&&j==n-1){
                return grid[i][j];
            }
            return grid[i][j]+Math.min(minpath(grid, i+1, j), minpath(grid, i, j+1));
        }
    }
  • 相关阅读:
    2-1 Restful中HTTP协议介绍
    11.修改WSDL文档
    10.TCPIP监听器
    05.使用jdk发布webservice服务
    09.ws复杂数据类型数据传输
    2019温馨的元旦祝福语 2019元旦祝福语大全!收藏备用!
    一文详解CSS常见的五大布局
    一文详解CSS常见的五大布局
    一文详解CSS常见的五大布局
    Asp.Net Core + Docker 搭建
  • 原文地址:https://www.cnblogs.com/haimishasha/p/11519156.html
Copyright © 2011-2022 走看看