8000 try to added python solution for no.1 · LeetCode-Interview/LeetCode@79c0d1c · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 79c0d1c

Browse files
committed
try to added python solution for no.1
1 parent 8de6f10 commit 79c0d1c

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

001. Single Number/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# 思路(C++)
2+
13
昨晚看C++ Primer 5th的[习题5.14](https://github.com/pezy/Cpp-Primer/blob/master/ch05/ex5_14.cpp)
24
想了很常规的解法,但脑子里一直都在想有没有更好的方案,因为这道题很像是在一篇文章里检索连续的高频词。应该是非常常见的才对。
35
但因为多了一个要求**连续**的条件,所以却又不太一样。
@@ -18,3 +20,9 @@ C++ Primer 5th ==> 4.8. The Bitwise Operators ==> Bitwise AND, OR, and XOR Opera
1820
异或(^)的特点是: **按位,同为0,异为1。**
1921

2022
所以这道题要找"落单"那哥们,就将他们全部异或,相同的小伙伴异或后为0了,可不就剩下那个老光棍了么。
23+
24+
## Python
25+
26+
心血来潮用 python 再刷一遍, 这道题有一种更简洁的写法是用 `reduce`. 为啥我只用了最简单的 `for` 循环呢?
27+
28+
请见 <https://stackoverflow.com/questions/181543/what-is-the-problem-with-reduce>

001. Single Number/main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@ int main()
88
Solution s;
99
int A[7] = {1,2,3,5,2,1,3};
1010
std::cout << s.singleNumber(A, 7) << std::endl;
11-
12-
return 0;
1311
}

001. Single Number/solution.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def singleNumber(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
single = 0
8+
for num in nums:
9+
single ^= num
10+
return single
11+
12+
13+
# test
14+
if __name__ == '__main__':
15+
print(Solution().singleNumber([2, 4, 5, 4, 5, 2, 6, 7, 7]))

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LeetCode solutions in C++ 11. (From Easy to Hard)
55

66
|NO.|Title|Solution|Add Date|Difficulty|
77
|---|-----|--------|--------|----------|
8-
|1|[Single Number][1]|[C++](001.%20Single%20Number/solution.h)|2014/10/15|Medium|
8+
|1|[Single Number][1]|[C++](001.%20Single%20Number/solution.h)&#124;[Python](001.%20Single%20Number/solution.py)|2014/10/15|Medium|
99
|2|[Maximum Depth of Binary Tree][2]|[C++](002.%20Maximum%20Depth%20of%20Binary%20Tree/solution.h)|2014/10/16|Easy|
1010
|3|[Same Tree][3]|[C++](003.%20Same%20Tree/solution.h)|2014/10/17|Easy|
1111
|4|[Reverse Integer][4]|[C++](004.%20Reverse%20Integer/solution.h)|2014/10/18|Easy|

0 commit comments

Comments
 (0)
0