在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ n passengers board an airplane with exactly n seats. The first passenger has lost the ticket and picks a seat randomly. But after that, the rest of passengers will: Take their own seat if it is still available, Example 1: Input: n = 1 Input: n = 2 Constraints: 1 <= n <= 10^5 有 n 位乘客即将登机,飞机正好有 n 个座位。第一位乘客的票丢了,他随便选了一个座位坐下。 剩下的乘客将会: 如果他们自己的座位还空着,就坐到自己的座位上, 当他们自己的座位被占用时,随机选择其他座位 示例 1: 输入:n = 1 输入: n = 2 提示: 1 <= n <= 10^5 Runtime: 4 ms
Memory Usage: 21.1 MB
1 class Solution { 2 func nthPersonGetsNthSeat(_ n: Int) -> Double { 3 if n == 1 4 { 5 return 1.0 6 } 7 else 8 { 9 return 0.5 10 } 11 } 12 }
|
请发表评论