[LeetCode]136.Single Numbe

【题目】

【解析】

在此我们利用异或的一个性质:

任何一个数字异或他自己都等于0。也就是说我们从头到尾异或数组中的每一个数字,

那么最终的结果刚好是哪个只出现一次的数字,因为那些成对出现的数字全部在异或中抵消了。

【代码】

class Solution {
public:
    int singleNumber(int A[], int n) {
        int i,result = 0;
        if(A == NULL || n <= 0){
            return -1;
        }
        for(i = 0;i < n;i++){
            result ^= A[i];
        }
        return result;
    }
};
/*********************************
*   日期:2013-12-04
*   作者:SJF0115
*   题目: 136.Single Number
*   网址:http://oj.leetcode.com/problems/single-number/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
#include <malloc.h>
#include <stdio.h>
using namespace std;

int *array;

int singleNumber(int A[], int n) {
    int i,result = 0;
    if(A == NULL || n <= 0){
        return -1;
    }
    for(i = 0;i < n;i++){
        result ^= A[i];
    }
    return result;
}

int main() {
    int i,n;
    while(scanf("%d",&n) != EOF){
        array = (int*)malloc(sizeof(int)*n);
        for(i = 0;i < n;i++){
            scanf("%d",&array[i]);
        }
        printf("%d\n",singleNumber(array,n));
    }//while
    return 0;
}

时间: 2024-09-21 21:45:27

[LeetCode]136.Single Numbe的相关文章

[LeetCode]--136. Single Number

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? 耐心看下我的思想演变过程吧,可能是大脑缺氧,用List装下标. public int

[LeetCode]*137.Single Number II

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数数组,每个元素出现了三次,除了一个.找出那

[经典面试题]位运算操作

[LeetCode]136.Single Numbe [LeetCode]201.Bitwise AND of Numbers Range [剑指Offer]40.数组中只出现一次的数字 一道位运算的算法题

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

[LeetCode] Single Number

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? 解题思路 亦或运算. 实现代码 // Runtime: 1 ms public cla

[LeetCode] Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解题思路 考虑全部用二进制表示,如果我们把 第 ith 个位置上所有数字的

[LeetCode] Single Number III

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The order

UVa 136 Ugly Numbers (数论)

136 - Ugly Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=72 Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2,

[LeetCode]135.Candy

[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can