[LeetCode]137. Single Number II

来源:互联网 发布:项目经理 it 编辑:程序博客网 时间:2024/06/06 03:51

https://leetcode.com/problems/single-number-ii/?tab=Description

一个数出现一次,剩余数都出现三次




int是32位,记录每一位上1出现的次数。套路题!

public class Solution {    public int singleNumber(int[] nums) {        int[] bit = new int[32];        for (int num : nums) {            for (int i = 0; i < 32; i++) {                bit[i] += ((num & (1 << i)) == 0 ? 0 : 1);            }        }        int res = 0;        for (int i = 0; i < 32; i++) {            res |= (bit[i] % 3) << i;        }        return res;    }}




0 0
原创粉丝点击