Skip to content

Commit a65d8b7

Browse files
author
Jack
committed
edit 4 solutions
1 parent 5e0d4d0 commit a65d8b7

File tree

5 files changed

+164
-5
lines changed

5 files changed

+164
-5
lines changed

.gitignore

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
.idea/
2+
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
env/
14+
build/
15+
develop-eggs/
16+
dist/
17+
downloads/
18+
eggs/
19+
.eggs/
20+
lib/
21+
lib64/
22+
parts/
23+
sdist/
24+
var/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.coverage
43+
.coverage.*
44+
.cache
45+
nosetests.xml
46+
coverage.xml
47+
*,cover
48+
.hypothesis/
49+
50+
# Translations
51+
*.mo
52+
*.pot
53+
54+
# Django stuff:
55+
*.log
56+
local_settings.py
57+
58+
# Flask stuff:
59+
instance/
60+
.webassets-cache
61+
62+
# Scrapy stuff:
63+
.scrapy
64+
65+
# Sphinx documentation
66+
docs/_build/
67+
68+
# PyBuilder
69+
target/
70+
71+
# IPython Notebook
72+
.ipynb_checkpoints
73+
74+
# pyenv
75+
.python-version
76+
77+
# celery beat schedule file
78+
celerybeat-schedule
79+
80+
# dotenv
81+
.env
82+
83+
# virtualenv
84+
venv/
85+
ENV/
86+
87+
# Spyder project settings
88+
.spyderproject
89+
90+
# Rope project settings
91+
.ropeproject

Python/compare-version-numbers.py

+29
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#
1818
# 0.1 < 1.1 < 1.2 < 13.37
1919
#
20+
import itertools
21+
2022

2123
class Solution(object):
2224
def compareVersion(self, version1, version2):
@@ -44,6 +46,8 @@ def compareVersion(self, version1, version2):
4446

4547
# Time: O(n)
4648
# Space: O(n)
49+
50+
4751
class Solution2(object):
4852
def compareVersion(self, version1, version2):
4953
"""
@@ -69,6 +73,31 @@ def compareVersion(self, version1, version2):
6973

7074
return 0
7175

76+
def compareVersion2(self, version1, version2):
77+
"""
78+
:type version1: str
79+
:type version2: str
80+
:rtype: int
81+
"""
82+
v1 = [int(x) for x in version1.split('.')]
83+
v2 = [int(x) for x in version2.split('.')]
84+
while len(v1) != len(v2):
85+
if len(v1) > len(v2):
86+
v2.append(0)
87+
else:
88+
v1.append(0)
89+
return cmp(v1, v2)
90+
91+
def compareVersion3(self, version1, version2):
92+
splits = (map(int, v.split('.')) for v in (version1, version2))
93+
return cmp(*zip(*itertools.izip_longest(*splits, fillvalue=0)))
94+
95+
def compareVersion4(self, version1, version2):
96+
main1, _, rest1 = ('0' + version1).partition('.')
97+
main2, _, rest2 = ('0' + version2).partition('.')
98+
return cmp(int(main1), int(main2)) or len(rest1 + rest2) and self.compareVersion4(rest1, rest2)
99+
100+
72101
if __name__ == "__main__":
73102
print Solution().compareVersion("21.0", "121.1.0")
74103
print Solution().compareVersion("01", "1")

Python/reverse-integer.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,28 @@ def reverse(self, x):
3535
x /= 10
3636
return result if result <= 0x7fffffff else 0 # Handle overflow.
3737

38-
38+
def reverse2(self, x):
39+
"""
40+
:type x: int
41+
:rtype: int
42+
"""
43+
if x < 0:
44+
x = int(str(x)[::-1][-1] + str(x)[::-1][:-1])
45+
else:
46+
x = int(str(x)[::-1])
47+
x = 0 if abs(x) > 0x7FFFFFFF else x
48+
return x
49+
50+
def reverse3(self, x):
51+
"""
52+
:type x: int
53+
:rtype: int
54+
"""
55+
s = cmp(x, 0)
56+
r = int(`s * x`[::-1])
57+
return s * r * (r < 2 ** 31)
58+
59+
3960
if __name__ == "__main__":
4061
print Solution().reverse(123)
4162
print Solution().reverse(-321)

Python/single-number.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
import operator
1111

12+
1213
class Solution:
13-
# @param A, a list of integer
14-
# @return an integer
14+
"""
15+
:type nums: List[int]
16+
:rtype: int
17+
"""
1518
def singleNumber(self, A):
1619
return reduce(operator.xor, A)
1720

1821
if __name__ == '__main__':
19-
print Solution().singleNumber([1, 1, 2, 2, 3])
22+
print Solution().singleNumber([1, 1, 2, 2, 3])

Python/two-sum.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# Because nums[0] + nums[1] = 2 + 7 = 9,
1313
# return [0, 1].
1414

15+
1516
class Solution(object):
1617
def twoSum(self, nums, target):
1718
"""
@@ -26,6 +27,20 @@ def twoSum(self, nums, target):
2627
lookup[num] = i
2728
return []
2829

30+
def twoSum2(self, nums, target):
31+
"""
32+
:type nums: List[int]
33+
:type target: int
34+
:rtype: List[int]
35+
"""
36+
k = 0
37+
for i in nums:
38+
j = target - i
39+
k += 1
40+
tmp_nums = nums[k:]
41+
if j in tmp_nums:
42+
return [k - 1, tmp_nums.index(j) + k]
43+
2944

3045
if __name__ == '__main__':
31-
print "index1=%d, index2=%d" % Solution().twoSum((2, 7, 11, 15), 9)
46+
print Solution().twoSum((2, 7, 11, 15), 9)

0 commit comments

Comments
 (0)