C语言 | Leetcode C语言题解之第260题只出现一次的数字III

题目:

题解:

cpp 复制代码
int* singleNumber(int* nums, int numsSize, int* returnSize) {
    int xorsum = 0;
    for (int i = 0; i < numsSize; i++) {
        xorsum ^= nums[i];
    }
    // 防止溢出
    int lsb = (xorsum == INT_MIN ? xorsum : xorsum & (-xorsum));
    int type1 = 0, type2 = 0;
    for (int i = 0; i < numsSize; i++) {
        int num = nums[i];
        if (num & lsb) {
            type1 ^= num;
        } else {
            type2 ^= num;
        }
    }

    int *ans = (int *)malloc(sizeof(int) * 2);
    ans[0] = type1;
    ans[1] = type2;
    *returnSize = 2;
    return ans;
}
相关推荐
Navigator_Z9 小时前
LeetCode //C - 1289. Minimum Falling Path Sum II
c语言·算法·leetcode
himobrinehacken10 小时前
取模运算的汇编奥秘揭秘
c语言·安全
mmmmath_310 小时前
LeetCode.104.二叉树的最大深度
数据结构·leetcode
青岚_Nox12 小时前
C语言实战:带你手写一个带"计时功能"的猜数字小游戏
c语言
Tisfy14 小时前
LeetCode 2333.最小差值平方和:排序 + 数学计算(不用二分)
数学·算法·leetcode·题解·贪心·排序
aqiu11111114 小时前
【C++算法打怪专栏】LeetCode 217. 存在重复元素(解法总结)
c++·算法·leetcode
iotxiaohu14 小时前
用 cflow 查看 C 函数调用关系
c语言·开发语言·cflow
yi01116 小时前
DAY28: LeetCode 111. 二叉树的最小深度
人工智能·笔记·python·算法·leetcode·二叉树·递归
局部有解17 小时前
Leetcode Hot100 - 最长连续序列
算法·leetcode·职场和发展
iCxhust17 小时前
51单片机电力载波应用开发(九)51产品化
c语言·数据库·嵌入式硬件·mysql·51单片机·软件工程·c51