当前位置:   article > 正文

华为OD面试题

华为od面试题

题目

hwOD.png


思路

方法一
- B座为空,判断A座、C座是否为空,一个座为空,TicketNums++
- D座为空,判断F座是否为空,为空,TicketNums++
  • 1
  • 2
方法二
当所有座位均可选时:TicketNums = rows * 2
- B座不为空,TicketNums--
- A座不为空并且B座为空,判断C座是否为空,不为空,TicketNums--
- C座不为空并且B座为空,判断A座是否为空,不为空,TicketNums--
- D座不为空并且F座为空,TicketNums--
- F座不为空并且D座为空,TicketNums--
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

实现

方法一
    public int getTicketNumsFew(int rows, List<String> seats){

        int[][] Seats = new int[rows+1][6];

        int num = 0;

        for (String seat : seats) {

            String[] chars = seat.split("");

            int row = Integer.parseInt(chars[0]);

            switch (chars[1]){
                case "A":
                    Seats[row][1] = 1;
                    break;
                case "B":
                    Seats[row][2] = 1;
                    break;
                case "C":
                    Seats[row][3] = 1;
                    break;
                case "D":
                    Seats[row][4] = 1;
                    break;
                case "F":
                    Seats[row][5] = 1;
                    break;
            }
        }

        for (int i = 1; i <= rows; i++){
            if (Seats[i][2] != 1 && (Seats[i][1] != 1 || Seats[i][3] != 1)){
                num++;
            }
            if (Seats[i][4] != 1 && Seats[i][5] != 1){
                num++;
            }
        }
        
        return num;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
方法二
    public int getTicketNumsLarge(int rows, List<String> seats){

        int[][] Seats = new int[rows+1][6];

        int num = rows * 2;

        for (String seat : seats) {

            String[] chars = seat.split("");

            int row = Integer.parseInt(chars[0]);

            switch (chars[1]){
                case "A":
                    Seats[row][1] = 1;
                    if (Seats[row][2] != 1 && Seats[row][3] == 1){
                        num--;
                    }
                    break;
                case "B":
                    Seats[row][2] = 1;
                    num--;
                    break;
                case "C":
                    Seats[row][3] = 1;
                    if (Seats[row][2] != 1 && Seats[row][1] == 1){
                        num--;
                    }
                    break;
                case "D":
                    Seats[row][4] = 1;
                    if (Seats[row][5] != 1){
                        num--;
                    }
                    break;
                case "F":
                    Seats[row][5] = 1;
                    if (Seats[row][4] != 1){
                        num--;
                    }
                    break;
            }
        }

        return num;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

原文地址:TT的博客《华为OD面试题》

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/625019
推荐阅读
相关标签
  

闽ICP备14008679号