1. 程式人生 > 其它 >【TcaplusDB知識庫】TcaplusDB 如何新增業務叢集cluster?

【TcaplusDB知識庫】TcaplusDB 如何新增業務叢集cluster?

給定一個整數陣列nums,其中恰好有兩個元素只出現一次,其餘所有元素均出現兩次。 找出只出現一次的那兩個元素。你可以按 任意順序 返回答案。

進階:你的演算法應該具有線性時間複雜度。你能否僅使用常數空間複雜度來實現?

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/single-number-iii
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

class Solution {
    public int[] singleNumber(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new int[0];
        }

        int all = 0;

        for (int num : nums) {
            all ^= num;
        }

        int lowbit = all & (-all);

        int one = 0, two = 0;

        for (int num : nums) {
            if ((num & lowbit) != 0) {
                one ^= num;
            } else {
                two ^= num;
            }
        }

        return new int[]{one, two};
    }
}
心之所向,素履以往 生如逆旅,一葦以航