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;
}
相关推荐
old_power1 小时前
UCRT 和 MSVC 的区别(Windows 平台上 C/C++ 开发相关)
c语言·c++·windows
緈福的街口2 小时前
【leetcode】3. 无重复字符的最长子串
算法·leetcode·职场和发展
@老蝴2 小时前
C语言 — 编译和链接
c语言·开发语言
LunaGeeking3 小时前
三分算法与DeepSeek辅助证明是单峰函数
c语言·c++·算法·编程·信奥赛·ai辅助学习·三分
小刘不想改BUG5 小时前
LeetCode 70 爬楼梯(Java)
java·算法·leetcode
whoarethenext6 小时前
使用 C/C++ 和 OpenCV 实现滑动条控制图像旋转
c语言·c++·opencv
sz66cm7 小时前
LeetCode刷题 -- 542. 01矩阵 基于 DFS 更新优化的多源最短路径实现
leetcode·矩阵·深度优先
陳麦冬8 小时前
深入理解指针(二)
c语言·学习
爱coding的橙子9 小时前
每日算法刷题Day24 6.6:leetcode二分答案2道题,用时1h(下次计时20min没写出来直接看题解,节省时间)
java·算法·leetcode
慢慢慢时光9 小时前
leetcode sql50题
算法·leetcode·职场和发展