GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Leetcode-Java/leetcode/solution/src/TotalHammingDistance.java at master · Tulpen/Leetcode-Java Expand file tree/ Copy pathTotalHammingDistance.javaMore file actions 23 lines (23 loc) · 645 Bytes / Copy pathTotalHammingDistance.javaFile metadata and controls23 lines (23 loc) · 645 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class TotalHammingDistance { /** * 思路是依次遍历每个数字的第i位,统计出为1的有m个,为0的有n个,则在这一位上 * 增加的hamming distance为m*n个 */ /** * 复杂度为O(n) */ public int totalHammingDistance(int[] nums) { int res = 0; for (int i = 0; i < 32; i++) { int a = 0; for (int j = 0; j < nums.length; j++) { if ((nums[j] & 1) != 0) { a++; } nums[j] >>>= 1; } res += a * (nums.length - a); } return res; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |