136. Single Number (Easy)

来源:互联网 发布:阿里云域名证书下载 编辑:程序博客网 时间:2024/05/21 11:56

Given an 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?

Subscribe to see which companies asked this question

Solutions:

C:

#include<stdio.h>int singleNumber(int* nums, int numsSize) {    int i;    int result = nums[0];    for(i = 1; i < numsSize; i++) {        result ^= nums[i];    }    return result;}int main() {    int arr[] = {2, 2, 5, 5, 6, 7, 7};    printf("%d", singleNumber(arr, 7));     return 0;} 

思路:使用^运算符,a^b^a=b

0 0
原创粉丝点击