代码拉取完成,页面将自动刷新
同步操作将从 doocs/leetcode 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
给你一个正整数 n
,开始时,它放在桌面上。在 109
天内,每天都要执行下述步骤:
x
,找出符合 1 <= i <= n
且满足 x % i == 1
的所有数字 i
。返回在 109
天之后,出现在桌面上的 不同 整数的数目。
注意:
%
表示取余运算。例如,14 % 3
等于 2
。
示例 1:
输入:n = 5 输出:4 解释:最开始,5 在桌面上。 第二天,2 和 4 也出现在桌面上,因为 5 % 2 == 1 且 5 % 4 == 1 。 再过一天 3 也出现在桌面上,因为 4 % 3 == 1 。 在十亿天结束时,桌面上的不同数字有 2 、3 、4 、5 。
示例 2:
输入:n = 3 输出:2 解释: 因为 3 % 2 == 1 ,2 也出现在桌面上。 在十亿天结束时,桌面上的不同数字只有两个:2 和 3 。
提示:
1 <= n <= 100
由于每一次对桌面上的数字 $n$ 进行操作,会使得数字 $n-1$ 也出现在桌面上,因此最终桌面上的数字为 $[2,...n]$,即 $n-1$ 个数字。
注意到 $n$ 可能为 $1$,因此需要特判。
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
class Solution:
def distinctIntegers(self, n: int) -> int:
return max(1, n - 1)
class Solution {
public int distinctIntegers(int n) {
return Math.max(1, n - 1);
}
}
class Solution {
public:
int distinctIntegers(int n) {
return max(1, n - 1);
}
};
func distinctIntegers(n int) int {
if n == 1 {
return 1
}
return n - 1
}
function distinctIntegers(n: number): number {
return Math.max(1, n - 1);
}
impl Solution {
pub fn distinct_integers(n: i32) -> i32 {
if n == 1 {
return 1;
}
n - 1
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。