- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
问题陈述如下:
给定一个包含 n 个整数的整数数组,找出可以由数组元素组成的所有对中的位差之和。一对 (x, y) 的位差是 x 和 y 的二进制表示中相同位置的不同位的计数。例如,2 和 7 的位差为 2。2 的二进制表示为 010,7 为 111(两个数的第一位和最后一位不同)。
例子:
Input: arr[] = {1, 2}
Output: 4
All pairs in array are (1, 1), (1, 2)
(2, 1), (2, 2)
Sum of bit differences = 0 + 2 +
2 + 0
= 4
基于 this post最有效(O(n) 的运行时间)解决方案如下:
The idea is to count differences at individual bit positions. We traverse from 0 to 31 and count numbers with i’th bit set. Let this count be ‘count’. There would be “n-count” numbers with i’th bit not set. So count of differences at i’th bit would be “count * (n-count) * 2″.
// C++ program to compute sum of pairwise bit differences
#include <bits/stdc++.h>
using namespace std;
int sumBitDifferences(int arr[], int n)
{
int ans = 0; // Initialize result
// traverse over all bits
for (int i = 0; i < 32; i++)
{
// count number of elements with i'th bit set
int count = 0;
for (int j = 0; j < n; j++)
if ( (arr[j] & (1 << i)) )
count++;
// Add "count * (n - count) * 2" to the answer
ans += (count * (n - count) * 2);
}
return ans;
}
// Driver prorgram
int main()
{
int arr[] = {1, 3, 5};
int n = sizeof arr / sizeof arr[0];
cout << sumBitDifferences(arr, n) << endl;
return 0;
}
我不完全清楚的是,当有两个 for 循环每次迭代递增 1 时,运行时间如何呈线性。我解释它的方式是因为外循环从 0 迭代到 32(对应于每个数字的第 0 位和第 32 位)并且因为我猜测所有 32 位移位都将在同一时钟周期内发生(或者与线性迭代相比相对较快),所以总体运行时间将由线性迭代决定在阵列上。
这是正确的解释吗?
最佳答案
在英语中,“My algorithm runs in O(n) time”翻译为“对于非常大的输入,我的算法运行时间最多与 n
成正比”。其比例方面是外循环中的 32 次迭代没有任何区别的原因。执行时间仍然与n成正比。
让我们看一个不同的例子:
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
// do something
}
}
在此示例中,执行时间与 n2 成正比,因此它不是 O(n)。然而它是 O(n2)。从技术上讲,O(n3) 和 O(n4),...也是如此。这是根据定义得出的。
你只能用英语谈论这些东西而不会被误解,所以如果你想确定这些概念,你最好查看介绍性算法教科书或在线类(class)中的正式定义,并制定一些示例。
关于arrays - 所有对之间的位差之和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39282387/
这个问题可能类似于In Angular2 *ngFor iteration, how do I output only unique values from the array?但我的问题是还有更多功
我编写了一个算法来获取 float 的总和,该算法对于整数来说非常有效,但是当我应用于 float 时,我得到的总和是负数。但是我的 float 数组只有正 float 。在这里我发布我的代码,感谢您
我想将这个简单的 for 循环转换为并行循环。它遍历字符串数组(从文本文件读取的 float )并计算总和。 for (int i = 0; i { float tmp; if (f
我正在尝试总结日期差异,一切都很好,除了如果有相同日期我想添加 1,例如,如果起始日期是:01/01/2003到目前为止是 01/01/2003 那么我想添加 1 天,但它没有添加 1 天,而是仅在
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Is JavaScript’s Floating-Point Math Broken? 这将是一个非常基本的计算机科
我刚接触sql,卡住了。我正在尝试计算每个用户走过的(每年)距离总和。我有一个具有以下结构的表(我们称之为 dist_table): rowid user_name date
我刚接触sql,卡住了。我正在尝试计算每个用户走过的(每年)距离总和。我有一个具有以下结构的表(我们称之为 dist_table): rowid user_name date
给定一个正数数组。我想将数组拆分为 2 个不同的子集,以使它们的 gcd(最大公约数)之和最大。 示例数组:{6,7,6,7}。 答案:需要的两个子集是:{6,6}和{7,7};它们各自的 gcd(s
我想在我的数组中求和:
我想将下面的字符串拆分为字母和数字,然后我需要计算数字的总和。我的示例问题是 a[20]={"abcd123dc2"}; 预期输出: abcddc 8 我的代码: int main() { c
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
我是一名优秀的程序员,十分优秀!