- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我提交到 leetcode 时,它运行案例 500/502 但失败了,原因:1808548329。但是当我在我自己的 mac 上运行它时,它给出了与接受的相同的答案。
我的代码:
int trailingZeroes(int n) {
int count = 0;
int tmp = 0; //check every number in [1, i]
for (int i = 1; i <= n; i++) {
tmp = i;
while (tmp % 5 == 0) {
count++;
tmp /= 5;
}
}
return count;
}
和 ac 答案:
int trailingZeroes2(int n) {
return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
}
他们在我的 mac 上运行相同的结果:
std::cout << trailingZeroes(1808548329) << std::endl; //452137076
std::cout << trailingZeroes2(1808548329) << std::endl; //452137076
第一个解不被接受的原因是时间复杂度吗?
(因为我在自己的 Mac 上运行它,但它给出的答案与 ac 给出的答案相同)
我如何计算第一个解决方案的时间复杂度,
是 O(NlogN)
吗?我不知道。你能帮我一个忙吗?:-)
编辑,删除图片。
最佳答案
您的解决方案是O(n)
。
将它加在一起得到内部循环运行:
n/5 + n/25 + n/125 + ... + 1 =
n (1/5 + 1/25 + 1/125 + ... + 1/n)
这是 sum of geometric series ,这是在 O(n)
此外,如果忽略内部循环,外部循环本身有 O(n) 次迭代,每次成本不变,所以这仍然是 O(n)
。
不过,替代解决方案在 O(logn)
中运行,效率要高得多。
关于algorithm - 阶乘尾随零 BigO 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50564984/
这个问题在这里已经有了答案: What is a plain English explanation of "Big O" notation? (43 个回答) 关闭 5 年前。 我刚刚开始阅读 G
这段代码的执行时间增长率Big O是多少? void doDiff(int setA[], int setB[], int sizeA, int sizeB) { const int MAX
我在网上阅读了一篇文章。根据我的理解,以下代码的 BigO 应该是 O(n)。因为循环运行了 n 次。但是文章中的正确答案显示为 O(1)。带说明 The code declares exactly
我目前正在努力解决算法问题。但我想我仍然不完全理解如何计算算法复杂度。我会说我的代码具有 O(n^3) 的复杂性,因为它们内部有三个主要循环在数据集上工作,有人可以确认这一点,或者如果我错了,请告诉我
T (1) = c T (n) = T (n/2) + dn 我如何快速确定 BigO? 最佳答案 使用重复的回代并找到模式。一个例子 here . 关于algorithm - 确定循环的 Bi
在证明和反证 Big O 问题中明确说明使用定义来证明和反证,我的问题是,我做的是正确的吗? 例如,您有一个问题是 g(n) = O(f(n)) ... 为了证明这一点,我做了以下操作 g(n) =
请注意,我没有“问题”,我也不是在寻找“另一种方法来找到我的算法的大 O”。 我想知道的是,是否有可能编写一个程序,您将向其传递数据点,这些数据点都是针对各种输入大小的算法的性能测量值:(n, tim
尝试对多大的系统进行线性回归是合理的? 具体来说:我有一个包含 ~300K 样本点和 ~1200 个线性项的系统。这在计算上可行吗? 最佳答案 线性回归计算为 (X'X)^-1 X'Y。 如果 X 是
当我提交到 leetcode 时,它运行案例 500/502 但失败了,原因:1808548329。但是当我在我自己的 mac 上运行它时,它给出了与接受的相同的答案。 我的代码: int tra
斯内普的《Unfriendly Algorithms for Wizards》教科书声称合并的运行时间排序是 O(n^4)。这种说法是否正确? 解决方案:是的。这个说法在技术上是正确的,因为 O(n^
好吧,这些都是非常简单的方法,而且有几个,所以当它们都是同一件事时,我不想只创建多个问题。 BigO 是我的弱点。我只是想不通他们是如何得出这些答案的。无论如何,您是否可以让我深入了解您对分析其中一些
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
AllDistinct(a1 , . . . , an ) if (n = 1) return True for i := n down to 2 begin if (LinearSear
那么如果一个函数或运行时间不是 f(n) 的大 O,我们可以说它是 f(n) 的大 Omega 吗? 最佳答案 没有。例如函数 / n^n if 2|n f(n) = |
sum = 0; for(i=0;i
我正在处理 water-collection between towers problem ,并尝试计算我的解决方案的 bigO 以供练习。 有一次,我从用户输入的高度数组构建了一个二维的“塔”数组。
修改 std::string 的运行时间是多少?我假设时间恒定,或者在字符串类的幕后发生了更多事情? #include int main() { std::string str = "Hello
有人知道 python 的字典“get(key)”方法的 O(?) 是多少吗? 我已经使用 cProfile 模块对其进行了测试,并在字典中获得了 100、1000、10000、100000、1000
谁能告诉我以下的 BigO: public void doFoo(int n) { int pass = 1; while (pass <= n) { for (int
我是一名优秀的程序员,十分优秀!