[抄题]:

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Now your job is to find the total Hamming distance between all pairs of the given numbers.

Example:

Input: 4, 14, 2

Output: 6

Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
showing the four bits relevant in this case). So the answer will be:
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为要在数字之间两两选择

[一句话思路]:

从32个格子的角度思考,个格子中做全排列

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. num_of_ones不是一次位运算出来的,而是逐步和第j位取&后 求和累加出来的

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

从32个格子的角度思考,32个格子中做全排列

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

class Solution {
public int totalHammingDistance(int[] nums) {
//cc
if (nums == null || nums.length == 0) return 0; //ini:
int res = 0; //for loop in 32 bit
for (int i = 0; i < 32; i++) {
int nums_of_ones = 0;
for (int j = 0; j < nums.length; j++)
nums_of_ones += (nums[j] >> i) & 1;
res += nums_of_ones * (nums.length - nums_of_ones);
} return res;
}
}

[代码风格] :

最新文章

  1. Android TextView中显示图片
  2. css 居中问题
  3. Bay Trail平板安装Ubuntu ThinkPad 8(20BNA00RCD)
  4. hibernate之关联映射
  5. 压测2.0:云压测 + APM = 端到端压测解决方案
  6. 学习笔记之高质量C++/C编程指南
  7. 这些年,我收集的JavaScript代码
  8. ueditor asp.net版本更改图片保存路径
  9. SQL:将查询结果插入到另一个表的三种情况
  10. css3图片模糊过滤特效
  11. 基于Java配置Spring加Hibernate和再加SpringData时的差别
  12. JQuery 常用的那些东西
  13. spring mvc controller中的参数验证机制(一)
  14. Canny边缘检测算法的一些改进
  15. 『编程题全队』Beta 阶段冲刺博客集合
  16. Springboot 打jar包分离lib,配置文件正确方式(二)
  17. java获得Tomcat服务器的根目录下的内容
  18. iOS 9 适配中出现的坑
  19. Leetcode 1022. Sum of Root To Leaf Binary Numbers
  20. Java之JVM逃逸分析

热门文章

  1. 理想中的 PCB 文件格式
  2. wordpress重力表单实时提醒功能教程(亲测可用)
  3. TCP/IP概念简述
  4. Android JNI中的数据传递
  5. Linnx 服务器中mysql 无法正常访问问题
  6. Appium ios新的定位方式FindsByIosNSPredicate (没有试 先记录在这里) 有个 driver.find_element_by_ios_uiautomation() 研究下 ios的定位
  7. 【AR实验室】mulberryAR:并行提取ORB特征
  8. java软件设计模式——单例设计模式中的【饿汉式】与 【懒汉式】示例
  9. Windows下安装GCC
  10. Dev GridControl 选择行及绑定/获取List对象