Sort Integers by the Number of 1 Bits

本文深入探讨了快速排序算法的实现,特别是在处理位运算时的优化技巧。通过自定义和标准库提供的位计数方法对比,展示了如何有效提升排序效率。文章提供了详细的代码示例,包括快速排序的分区函数和比较逻辑,以及两种位计数方法的性能对比。

快排,注意比较的方法,因为 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;
}
c++题目,代码无注释 T-2 Binary String and Score 分数 35 作者 曹鹏 单位 Amazon Given a binary string s (contains only characters 0 and 1), here is the way we calculate its score: Cut the string into several groups, each group contains the same character and consecutive groups contain different characters. The score is defined as the exclusive-or value of all the group lengths (if there is only one group, the score is its length). For instance, for string “10001100001”, the groups are “1”, “000”, “11”, “0000” and “1”. The score is 1 ^ 3 ^ 2 ^ 4 ^ 1 = 5. Now in each step, you can change the string by in swapping any adjacent 2 characters. You can apply as many as steps as you want (possible 0). For each possible score, how many different strings can you get and what’s the minimal number of steps to change s into a string with the particular score? Input Specification: Each input file contains one test case. Each case has a single line containing the binary string s (1 ≤ s.length() ≤ 64). Output Specification: For each possible score, output a line containing 3 space-separated integers, namely, the score, the number of different strings with the particular score you can get after changing s as many times as you want, and the minimal number of steps to change s into a string of that score. Output the lines in the score’s ascending order. Sample Input: 010 Sample Output: 1 1 0 3 2 1 Hint: There are 3 possible final strings in total. “010” needs has score 1 (1 ^ 1 ^ 1 = 1) and the minimal number of steps to get it is 0. Both “011” and “110” have score 3 (1 ^ 2) and the minimal number of steps to get them is 1. (swap the middle ‘1’ with the character on the left or right side.) 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB
08-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值