赞
踩
- B座为空,判断A座、C座是否为空,一个座为空,TicketNums++
- D座为空,判断F座是否为空,为空,TicketNums++
当所有座位均可选时:TicketNums = rows * 2
- B座不为空,TicketNums--
- A座不为空并且B座为空,判断C座是否为空,不为空,TicketNums--
- C座不为空并且B座为空,判断A座是否为空,不为空,TicketNums--
- D座不为空并且F座为空,TicketNums--
- F座不为空并且D座为空,TicketNums--
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; }
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; }
原文地址:TT的博客《华为OD面试题》
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。