Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
LeetCodeAnimation
Commits
d9bf5191
Unverified
Commit
d9bf5191
authored
Jul 29, 2020
by
Zong
Committed by
GitHub
Jul 29, 2020
Browse files
Update 0136-Single-Number.md
添加C、C++、Java、python代码实现
parent
3daa1877
Changes
1
Hide whitespace changes
Inline
Side-by-side
0136-Single-Number/Article/0136-Single-Number.md
View file @
d9bf5191
...
...
@@ -51,6 +51,62 @@

### 代码实现
#### C
````
c
int
singleNumber
(
int
*
nums
,
int
numsSize
){
int
res
=
0
;
for
(
int
i
=
0
;
i
<
numsSize
;
i
++
)
{
res
^=
nums
[
i
];
}
return
res
;
}
````
#### C++
````
c++
class
Solution
{
public:
int
singleNumber
(
vector
<
int
>&
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 @@

\ No newline at end of file

Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment