PTA:Single Number (15分)(C语言)

Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Tip:

You can use bitwise to solve this problem.

函数接口定义:
long long singleNumber(long long* nums, int numsSize);

裁判测试程序样例:
#include <stdio.h>
#define MAXN 1000005

long long nums[MAXN];

long long singleNumber(long long* nums, int numsSize);

int main()
{

int N;

scanf("%d", &N);
for(int i = 0; i < N; i++)
scanf("%lld", &nums[i]);

printf("%lld", singleNumber(nums, N));

return 0;

}

/* 请在这里填写答案 */

输入样例1:
3
2 2 1

输出样例1:
1

输入样例2:
5
4 1 2 1 2

输出样例2:
4

long long singleNumber(long long* nums, int numsSize)
{
    int i;
    long long ans = nums[0];
    for (i = 1; i < numsSize; i++)
        ans = ans ^ nums[i];
    return ans;

}
发布了58 篇原创文章 · 获赞 21 · 访问量 605

猜你喜欢

转载自blog.csdn.net/qq_45624989/article/details/105399578