From d9bf519107dc04c868041f32db6db65f4ef514cf Mon Sep 17 00:00:00 2001 From: Zong <1003160664@qq.com> Date: Wed, 29 Jul 2020 15:49:56 +0800 Subject: [PATCH] Update 0136-Single-Number.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加C、C++、Java、python代码实现 --- .../Article/0136-Single-Number.md | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/0136-Single-Number/Article/0136-Single-Number.md b/0136-Single-Number/Article/0136-Single-Number.md index 3f778c6..8bae68b 100644 --- a/0136-Single-Number/Article/0136-Single-Number.md +++ b/0136-Single-Number/Article/0136-Single-Number.md @@ -51,6 +51,62 @@ ![](../Animation/136.gif) +### 代码实现 +#### C +````c +int singleNumber(int* nums, int numsSize){ + int res=0; + for(int i=0;i& nums) { + int res=0; + for(auto n:nums) + { + // 异或 + res ^= n; + } + return res; + } +}; +```` + +#### Java +````java +class Solution { + public int singleNumber(int[] nums) { + int res = 0; + for(int n:nums) + { + // 异或 + res ^= n; + } + return res; + } +} +```` + +#### pyton +````python +class Solution(object): + def singleNumber(self, nums): + return reduce(lambda x,y:x^y, nums) +# reduce用法举例 +# 计算列表和:1+2+3+4+5 +# 使用 lambda 匿名函数 +# reduce(lambda x, y: x+y, [1,2,3,4,5]) +```` + ### 进阶版 有一个 n 个元素的数组,除了两个数只出现一次外,其余元素都出现两次,让你找出这两个只出现一次的数分别是几,要求时间复杂度为 O(n) 且再开辟的内存空间固定(与 n 无关)。 @@ -83,4 +139,4 @@ -![](../../Pictures/qrcode.jpg) \ No newline at end of file +![](../../Pictures/qrcode.jpg)