From 5c8e809de03d85886606595b46a8e3cbe45d3cc5 Mon Sep 17 00:00:00 2001 From: Zong <1003160664@qq.com> Date: Fri, 31 Jul 2020 16:19:50 +0800 Subject: [PATCH] Update 0137-Single-Number-II.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加C++、Java、Python代码实现 --- .../Article/0137-Single-Number-II.md | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/0137-Single-Number-II/Article/0137-Single-Number-II.md b/0137-Single-Number-II/Article/0137-Single-Number-II.md index 0c82b30..245e9ab 100644 --- a/0137-Single-Number-II/Article/0137-Single-Number-II.md +++ b/0137-Single-Number-II/Article/0137-Single-Number-II.md @@ -46,5 +46,45 @@ ![](../Animation/137.gif) +### 代码实现 +#### C++ +```c++ +class Solution { +public: + int singleNumber(vector& nums) { + int one=0, two=0; + for(int n:nums) + { + one = (one ^ n) & (~two); + two = (two ^ n) & (~one); + } + return one; + } +}; +``` +#### Java +```java +class Solution { + public int singleNumber(int[] nums) { + int one=0, two=0; + for(int n:nums) + { + one = (one ^ n) & (~two); + two = (two ^ n) & (~one); + } + return one; + } +} +``` +#### Python +```python +class Solution(object): + def singleNumber(self, nums): + one = two = 0 + for n in nums: + one = (one ^ n) & (~two) + two = (two ^ n) & (~one) + return one +``` -![](../../Pictures/qrcode.jpg) \ No newline at end of file +![](../../Pictures/qrcode.jpg)