[LeetCode136]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?

中文(给定一个整数的数组,每个整数都出现了两次,只有一个出现了一次,找到它  建议不使用额外的内存空间)

思路:利用^运算符  

二元 ^ 运算符是为整型和 bool 类型预定义的。对于整型,^ 将计算操作数的按位“异或”。对于 bool 操作数,^ 将计算操作数的逻辑“异或”;也就是说,当且仅当只有一个操作数为 true 时,结果才为 true。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeetCode
{
    class SingleNumberSolution
    {
        //static void Main()
        //{
        //    SingleNumberSolution s = new SingleNumberSolution();
        //    int[] nums = {1, 1, 2, 3, 3, 4, 4};
        //    Console.WriteLine(s.SingleNumber(nums));
        //}

        public int SingleNumber(int[] nums)
        {
            int singleNum = nums[0];
            for (int i = 1; i < nums.Length; i++)
            {
                singleNum ^= nums[i];
            }
            return singleNum;
        }
    }
}
时间: 2024-11-06 22:17:59

[LeetCode136]Single Number的相关文章

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,

LeetCode136——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? 难度系数: 中等 实现 做了SingleNumberII之后,就发现这题实在是再简单不

leetcode136:single number I&II 及拓展

Single Number I 给定一个数组,除了一个元素出现一次之外,其余的元素都出现两次.找出这个元素 分析: 只要将所有的元素都异或一次,最后的结果就是这个出现一次数. 代码: int singleNumber(int A[], int n) { int num; int i; num=A[0]; for(i=1;i<n;i++) { num=num^A[i]; } return num; } Single Number II 给定一个数组,除了一个元素出现一次之外,其余的元素都出现三次.

Configuring Single Number Reach on Cisco CUCM 7.1.3

Cisco CUCM allows you to configure single number reach (also known as mobility) so that a call destined for your desk phone's extension can ring on a number of other devices such as your cellphone, blackberry, home phone, etc.  This feature also allo

260. Single Number III

260. Single Number III DescriptionHintsSubmissionsDiscussSolution DiscussPick One 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 o

Single Number II

题目描写叙述 链接地址 解法 算法解释 题目描写叙述 Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Example Given [1,1,2,3,3,3,2,2,4,1] return 4 Challenge One-pass, constant extra space. 链接地址 http://www.lintcode.com/en/problem/single-number-ii/

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? 思路: 一开始自己的思路非常简单,首先设置一个status的变量,然后通过2层for循环

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description 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

LeetCode 136、137:Single Number I &amp; II

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? Single Number II Given an arr