一、题目
1、题目描述
给你一个正整数
n
,开始时,它放在桌面上。在109
天内,每天都要执行下述步骤:
- 对于出现在桌面上的每个数字
x
,找出符合1 <= i <= n
且满足x % i == 1
的所有数字i
。- 然后,将这些数字放在桌面上。
返回在
109
天之后,出现在桌面上的 不同 整数的数目。注意:
- 一旦数字放在桌面上,则会一直保留直到结束。
%
表示取余运算。例如,14 % 3
等于2
。
2、接口描述
python3
class Solution:
def distinctIntegers(self, n: int) -> int:
cpp
class Solution {
public:
int distinctIntegers(int n) {
}
};
3、原题链接
二、解题报告
1、思路分析
对于任意数字x >= 2,有x % x - 1 = 1,而1e9 >> 100,故2 ~ n都能包含进来,所以当n > 1时,答案为n - 1,对于1特判为1
2、复杂度
时间复杂度: O(1)空间复杂度:O(1)
3、代码详解
cpp
class Solution {
public:
int distinctIntegers(int n) {
return n > 1 ? n - 1 : 1;
}
};
python3
class Solution:
def distinctIntegers(self, n: int) -> int:
return n - 1 if n > 1 else 1