public class Solution {
public int SingleNumber(int[] nums) {
Dictionary<int, int> dic = new Dictionary<int, int>();
foreach (var n in nums)
{
if (!dic.ContainsKey(n))
{
dic.Add(n, );
}
else
{
dic[n]++;
}
} var result = ; foreach (var d in dic)
{
if (d.Value == )
{
result = d.Key;
}
}
//Console.WriteLine(result);
return result;
}
}

https://leetcode.com/problems/single-number/#/description

C++代码:

class Solution {
public:
int singleNumber(vector<int>& nums) {
set<int> SET;
for (auto n : nums)
{
if (SET.find(n) == SET.end())
{
SET.insert(n);
}
else
{
SET.erase(n);
}
}
for (auto s : SET)
{
return s;
}
}
};

补充一个python的实现:

 class Solution:
def singleNumber(self, nums: List[int]) -> int:
a =
for i in nums:
a ^= i
return a

原理:x ^ 0 = x,x ^ x = 0

leetcode136的更多相关文章

  1. [LeetCode136]Single Number寻找一个数组里只出现一次的数

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  2. leetcode136 利用异或运算找不同的元素

    Given an array of integers, every element appears twice except for one. Find that single one. Note: ...

  3. LeetCode136.只出现一次的数字

    给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: 输入: [ ...

  4. [Swift]LeetCode136. 只出现一次的数字 | Single Number

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

  5. leetcode136只出现一次的数字

    交换律:a ^ b ^ c <=> a ^ c ^ b 任何数于0异或为任何数 0 ^ n => n 相同的数异或为0: n ^ n => 0 int singleNumber ...

  6. Leetcode--136. Single Number(easy)

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

  7. LeetCode136:Single Number

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  8. leetcode136 Single Number

    题意:数组中每个数字都出现了两次,只有一个出现一次,找出这个数 思路:很明显不能从头到位遍历来找,首先是超时的原因,再次就是这样很没意思·····但是却没想到什么好办法,因为不了解按位异或(XOR). ...

  9. LeetCode136,137寻找只出现一次的数

    1.题目意思:在数组中,只有一个数字只出现了一次 其他的都出现了两次.找出那个只出现一次的数字. //利用位运算 异或 两个相同的数字异或为0 public int singleNumber(int[ ...

随机推荐

  1. STL next_permutation 算法原理和实现

    转载自:https://www.cnblogs.com/luruiyuan/p/5914909.html 目标 STL中的next_permutation 函数和 prev_permutation 两 ...

  2. Python基础10_函数

    直接贴笔记 : #!/usr/bin/env python # coding:utf-8 # 定义函数时要写成良好的注释习惯 通常用三个单引号 def test(x): ''' 计算一个y=2*x+1 ...

  3. disjoint set

    MAKE-SET.x/ creates a new set whose only member (and thus representative) is x. Since the sets are d ...

  4. C#中字符串大小比较函数--CompareTo与Compare方法(需要完善补充)

    字符串比较的原理是什么? 原理: 从两个字符串的第一个字符开始逐个进行比较(按字符的ASCII值进行大小比较),直到出现不同的字符或遇到‘\0’为止. 如果全部字符都相同,就认为两字符串相等,返回0: ...

  5. gitlab修改默认端口

    部署gitlab的时候,一启动,发现80和8080端口已经被占用,无奈,只得先将监听80端口的nginx和监听8080端口的jenkins停止.这会儿有空,琢磨一下如何修改gitlab的默认端口. 修 ...

  6. 关于LaTeX公式排版

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2017/10/05 1.居中括号框住多行公式 \begin{equation*} \left\{\begin{alig ...

  7. Json序列化,有多对一和多对多关系时出现的问题

    /** * 这是一张 单表中的双向一对多,多对一 关系.自己跟自己一对多多对一 */ @Entity @Table(name="tb_test") public class Cre ...

  8. Centos7 安装nginx1.14

    一丶官网 http://nginx.org/en/download.html 至于安装那个版本首先要看清楚版本代表什么意思 Nginx官网提供了三个类型的版本Mainline version:Main ...

  9. html对a标签的运用以及属性,img图像标签的属性及应用

    今天学习的难点自我感觉在于a标签超链接的应用.不是很熟练,晚上回家准备敲敲代码,让a的超链接标签使用的更加熟练,对于上午的img 属性值已经明白 . 还是日常记一下每日的重点   a标签去下划线:a{ ...

  10. SQLServer调WebService & 错误解决:请求格式无法识别

    (sqlServer 2008 + VS2010) 首先,对服务器进行配置. sp_configure ; GO RECONFIGURE; GO sp_configure ; GO RECONFIGU ...