- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我正在阅读这个网站的一些代码:http://www.geeksforgeeks.org/write-a-c-program-to-find-the-parity-of-an-unsigned-integer/
它展示了如何判断一个数是偶数还是奇数。但是,我不明白为什么运行时效率是log(n)。以下是引用代码:
# include <stdio.h>
# define bool int
/* Function to get parity of number n. It returns 1
if n has odd parity, and returns 0 if n has even
parity */
bool getParity(unsigned int n)
{
bool parity = 0;
while (n)
{
parity = !parity;
n = n & (n - 1);
}
return parity;
}
最佳答案
运行时效率为 O(log(n)),其中 n
是您传入的整数的值。但是,这是一种非常规的 O(log(n)) 使用方式符号。
更常见的是,O 表示法根据输入的大小 以位表示(表示输入所需的位数),在这种情况下,输入的大小为 k= O(log2(n)),运行时间为 O(k)。
(更准确地说,运行时间是 Θ(s),其中 s 是 n 中设置的位数,尽管假设位操作是 O(1))。
关于c - 为什么一维奇偶校验的大O运行时是log(n)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30952512/
我在我的服务器上本地配置了 MySQL。没有远程连接。然而,它是“写入网络”——知道我如何弄清楚发生了什么吗?我在 CloudLinux 7 上运行 MariaDB 10.1。谢谢! 10583 ab
我有这样一个类: class A { public: A(); ~A(); bool init(); private:
我正在尝试从 official documentation 学习 Typescript .而在 Interfaces section我已阅读以下内容: TypeScript comes with a
我是一名优秀的程序员,十分优秀!