C语言 | Leetcode C语言题解之第260题只出现一次的数字III
题目:
题解:
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;
}
原文地址:https://blog.csdn.net/m0_59237910/article/details/140564222
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!