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;
}
相关推荐
czlczl2002092515 分钟前
算法:组合问题
算法·leetcode·职场和发展
CoderYanger18 分钟前
优选算法-字符串:63.二进制求和
java·开发语言·算法·leetcode·职场和发展·1024程序员节
charlie11451419125 分钟前
深入理解C/C++的编译链接技术6——A2:动态库设计基础之ABI设计接口
c语言·开发语言·c++·学习·动态库·函数
Cx330❀28 分钟前
C++ STL set 完全指南:从基础用法到实战技巧
开发语言·数据结构·c++·算法·leetcode·面试
white-persist31 分钟前
【攻防世界】reverse | Reversing-x64Elf-100 详细题解 WP
c语言·开发语言·网络·python·学习·安全·php
Want5951 小时前
C/C++贪吃蛇小游戏
c语言·开发语言·c++
2501_941805313 小时前
智慧零售平台中的多语言语法引擎与实时推荐系统实践
leetcode
雨落在了我的手上3 小时前
C语言入门(二十二):字符函数和字符串函数(2)
c语言
qq_401700416 小时前
嵌入式用Unix时间的优势及其C语言转换
服务器·c语言·unix
CoderYanger9 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节