题解 | #数组中只出现一次的两个数字#

数组中只出现一次的两个数字

https://www.nowcoder.com/practice/389fc1c3d3be4479a154f63f495abff8

如高赞答案所说,这是c语言的解法,记录一下这个巧妙的思路
int* FindNumsAppearOnce(int* array, int arrayLen, int* returnSize ) {
    // write code here
    int* nums = (int*)malloc(sizeof(int)*2);
    int num = 0;
    int count = 1;
    for(int i = 0;i<arrayLen;i++)
    {
        num ^= array[i];   
    }
    while((count&num) == 0)
    {
        count <<= 1;
    }
    int a = 0;
    int b = 0;
    for(int j=0; j<arrayLen; j++){
        if((array[j]&count) != 0){
            a ^= array[j];
        }else{
            b ^= array[j];
        }
    }

    if(a > b){
        int c = a;
        a = b;
        b = c;
    }
    nums[0]=a;
    nums[1]=b;
    
    *returnSize = 2;
    return nums;
    
}

#个人随笔记##题解#
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务