快排,注意比较的方法,因为 bitCount 相同时也要排序。

class Solution {public int[] sortByBits(int[] arr) {if (arr == null || arr.length == 0) {return new int[0];}quickSort(arr, 0, arr.length-1);return arr;}private void quickSort(int[] arr, int l, int r) {if (l < r) {int mid = partition(arr, l, r);quickSort(arr, l, mid-1);quickSort(arr, mid+1, r);}}private int partition(int[] arr, int l, int r) {int key = arr[r];int i = l - 1;for (int j=l; j<r; j++) {if (smaller(arr[j], key)) {i++;int tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;}}i++;int tmp = arr[i];arr[i] = arr[r];arr[r] = tmp;return i;}private boolean smaller(int i, int j) {int iCount = Integer.bitCount(i);int jCount = Integer.bitCount(j);if (iCount < jCount) {return true;}if (iCount == jCount) {return i < j;}return false;}
}

使用我自己写的 bitCount方法,是 10ms,使用 Integer.bitCount方法是 3ms。

private int bitCount(int i) {int result = 0;while (i != 0) {if ((i & 1) == 1) {result++;}i >>>= 1;}return result;
}

Integer.bitCount

public static int bitCount(int i) {i = i - ((i >>> 1) & 0x55555555);i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);i = (i + (i >>> 4)) & 0x0f0f0f0f;i = i + (i >>> 8);i = i + (i >>> 16);return i & 0x3f;
}

Sort Integers by the Number of 1 Bits相关推荐

  1. 1356. Sort Integers by The Number of 1 Bits 根据数字二进制下 1 的数目排序

    给你一个整数数组 arr .请你将数组中的元素按照其二进制表示中数字 1 的数目升序排序. 如果存在多个数字二进制中 1 的数目相同,则必须将它们按照数值大小升序排列. 请你返回排序后的数组.   示 ...

  2. [LeetCode] Number of 1 Bits Reverse Integer - 整数问题系列

    目录: 1.Number of 1 Bits  - 计算二进制1的个数 [与运算] 2.Contains Duplicate - 是否存在重复数字 [遍历] 3.Reverse Integer - 翻 ...

  3. LeetCode - Easy - 191. Number of 1 Bits

    Topic Bit Manipulation Description https://leetcode.com/problems/number-of-1-bits/ Write a function ...

  4. LeetCode(191 461):位1的个数 Number of 1 Bits 汉明距离 Hamming Distance(Java)

    2019.9.14 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) github:https://github.com/ChopinXBP/LeetCode-Babel 之前在 ...

  5. LeetCode 191 Number of 1 Bits

    LeetCode 191 Number of 1 Bits 解法一(较为传统都解法):使用将n不断右移,并与1想&得到1的个数:(也有使用除法/2的,明显除法的运行效率要低于位移) 时间复杂度 ...

  6. 领扣-191 位1的个数 Number of 1 Bits MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. leetcode 191. Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of '1' bits it has (also know ...

  8. Leetcode PHP题解--D57 762. Prime Number of Set Bits in Binary Representation

    2019独角兽企业重金招聘Python工程师标准>>> D57 762. Prime Number of Set Bits in Binary Representation 题目链接 ...

  9. 【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)

    题目 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also k ...

  10. leetcode Number of 1 Bits

    题目连接 https://leetcode.com/problems/number-of-1-bits/ Number of 1 Bits Description Write a function t ...

最新文章

  1. 让 Spring Boot 启动更快一点
  2. java中JScrollPane不显示水平滚动条的解决办法
  3. SpringMVC+Mybatis学习
  4. python函数变量的作用域_学不会的Python函数——变量作用域
  5. js setTimeout和setInterval区别
  6. nginx 一个请求发给多台机器_一个机器人可以同时为多台数控机床上下料吗?东智力衡...
  7. C#LeetCode刷题之#7-反转整数(Reverse Integer)
  8. linux下启动Oracle服务和监听程序
  9. 每日一记:win10无法设置上网代理
  10. 手机号码或账号查询支付宝名字和性别
  11. 如何在word中同一位置处插入多篇连续文献
  12. Qt中出现 exited with code 3错误的其中之一原因
  13. java格林威治时间转北京时间
  14. Nightmare2
  15. 【动网论坛7.1 sp1 修改】-修改搜一搜为其他搜索的方法
  16. 开发嵌入京东app h5| Hybrid | 微信小程序 | 实践踩坑总结十六条
  17. iosepub阅读器_iOS苹果手机上最好的3个mobi阅读器
  18. 黑马程序员-java-小天教你使用eclipse
  19. 对Linux支持好的硬件厂商,浅析三大主流显卡厂商对 Linux 的驱动支持
  20. 用kali在有password的情况下暴力破解加密的pdf

热门文章

  1. maven项目的构建
  2. oracle参数文件spfile和pfile
  3. CSS中filter滤镜的学习笔记
  4. 华为note10能更新鸿蒙系统吗,华为官方再次确认!荣耀手机也能升级鸿蒙OS系统:内测机型遭到曝光...
  5. apache配置本地的虚拟主机
  6. 计算机将在1分钟重启,Win10开机经常提示将在一分钟后自动重启如何处理
  7. 木门怎么打_装门框,只打发泡剂一点不牢固!如今门套下沉,气得邻居直跺脚!...
  8. 高效能人士的七个习惯_如何成为高效能人士?——《高效能人士的七个习惯》读后感...
  9. php下一页的代码,php 实现文章上一页与下一页 代码
  10. python类封装成dl_第7.9节 案例详解:Python类封装