Skip to content

Commit 5ed5a1d

Browse files
author
Jack
committed
edit some solutions
1 parent a65d8b7 commit 5ed5a1d

11 files changed

+198
-18
lines changed

Python/add-digits.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@
1717
#
1818
# A naive implementation of the above process is trivial.
1919
# Could you come up with other methods?
20-
#
20+
21+
2122
class Solution:
22-
# @param {integer} num
23-
# @return {integer}
23+
"""
24+
:type num: int
25+
:rtype: int
26+
"""
2427
def addDigits(self, num):
2528
return (num - 1) % 9 + 1 if num > 0 else 0
29+
30+
31+
if __name__ == '__main__':
32+
s = Solution()
33+
r = s.addDigits(12345)
34+
print r

Python/battleships-in-a-board.py

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
# This is not a valid board - as battleships will always have a cell separating between them.
2525
# Your algorithm should not modify the value of the board.
2626

27+
2728
class Solution(object):
2829
def countBattleships(self, board):
2930
"""

Python/counting-bits.py

+17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# 3. Or does the odd/even status of the number help you in
2626
# calculating the number of 1s?
2727

28+
2829
class Solution(object):
2930
def countBits(self, num):
3031
"""
@@ -36,3 +37,19 @@ def countBits(self, num):
3637
# Number of 1's in i = (i & 1) + number of 1's in (i / 2).
3738
res.append((i & 1) + res[i >> 1])
3839
return res
40+
41+
def countBits2(self, num):
42+
"""
43+
:type num: int
44+
:rtype: List[int]
45+
"""
46+
s = [0]
47+
while len(s) <= num:
48+
s.extend(map(lambda x: x + 1, s))
49+
return s[:num + 1]
50+
51+
52+
if __name__ == '__main__':
53+
s = Solution()
54+
r = s.countBits2(5)
55+
print r

Python/find-all-numbers-disappeared-in-an-array. Python/find-all-numbers-disappeared-in-an-array.py

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# Output:
1818
# [5,6]
1919

20+
2021
class Solution(object):
2122
def findDisappearedNumbers(self, nums):
2223
"""
@@ -34,3 +35,23 @@ def findDisappearedNumbers(self, nums):
3435
else:
3536
nums[i] *= -1
3637
return result
38+
39+
def findDisappearedNumbers2(self, nums):
40+
"""
41+
:type nums: List[int]
42+
:rtype: List[int]
43+
"""
44+
return list(set(range(1, len(nums) + 1)) - set(nums))
45+
46+
def findDisappearedNumbers3(self, nums):
47+
for i in range(len(nums)):
48+
index = abs(nums[i]) - 1
49+
nums[index] = - abs(nums[index])
50+
51+
return [i + 1 for i in range(len(nums)) if nums[i] > 0]
52+
53+
54+
if __name__ == '__main__':
55+
s = Solution()
56+
r = s.findDisappearedNumbers([4, 3, 2, 7, 8, 2, 3, 1])
57+
print r

Python/find-the-difference.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
# Explanation:
2121
# 'e' is the letter that was added.
2222

23-
from operator import xor
23+
import operator
24+
import collections
25+
2426

2527
class Solution(object):
2628
def findTheDifference(self, s, t):
@@ -29,4 +31,32 @@ def findTheDifference(self, s, t):
2931
:type t: str
3032
:rtype: str
3133
"""
32-
return chr(reduce(xor, map(ord, s), 0) ^ reduce(xor, map(ord, t), 0))
34+
return chr(reduce(operator.xor, map(ord, s), 0) ^ reduce(operator.xor, map(ord, t), 0))
35+
36+
def findTheDifference2(self, s, t):
37+
"""
38+
:type s: str
39+
:type t: str
40+
:rtype: str
41+
"""
42+
t = list(t)
43+
s = list(s)
44+
for i in s:
45+
t.remove(i)
46+
return t[0]
47+
48+
def findTheDifference3(self, s, t):
49+
return chr(reduce(operator.xor, map(ord, s + t)))
50+
51+
def findTheDifference4(self, s, t):
52+
return list((collections.Counter(t) - collections.Counter(s)))[0]
53+
54+
def findTheDifference5(self, s, t):
55+
s, t = sorted(s), sorted(t)
56+
return t[-1] if s == t[:-1] else [x[1] for x in zip(s, t) if x[0] != x[1]][0]
57+
58+
59+
if __name__ == '__main__':
60+
s = Solution()
61+
r = s.findTheDifference2('abcd', 'abcde')
62+
print r

Python/fizz-buzz.py

+23
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,26 @@ def fizzBuzz(self, n):
4949
result.append(str(i))
5050

5151
return result
52+
53+
def fizzBuzz2(self, n):
54+
"""
55+
:type n: int
56+
:rtype: List[str]
57+
"""
58+
l = [str(x) for x in range(n + 1)]
59+
l3 = range(0, n + 1, 3)
60+
l5 = range(0, n + 1, 5)
61+
for i in l3:
62+
l[i] = 'Fizz'
63+
for i in l5:
64+
if l[i] == 'Fizz':
65+
l[i] += 'Buzz'
66+
else:
67+
l[i] = 'Buzz'
68+
return l[1:]
69+
70+
def fizzBuzz3(self, n):
71+
return ['Fizz' * (not i % 3) + 'Buzz' * (not i % 5) or str(i) for i in range(1, n + 1)]
72+
73+
def fizzBuzz4(self, n):
74+
return ['FizzBuzz'[i % -3 & -4:i % -5 & 8 ^ 12] or `i` for i in range(1, n + 1)]

Python/move-zeroes.py

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# You must do this in-place without making a copy of the array.
1313
# Minimize the total number of operations.
1414

15+
1516
class Solution(object):
1617
def moveZeroes(self, nums):
1718
"""
@@ -24,6 +25,13 @@ def moveZeroes(self, nums):
2425
nums[i], nums[pos] = nums[pos], nums[i]
2526
pos += 1
2627

28+
def moveZeroes2(self, nums):
29+
"""
30+
:type nums: List[int]
31+
:rtype: void Do not return anything, modify nums in-place instead.
32+
"""
33+
nums.sort(cmp=lambda a, b: 0 if b else -1)
34+
2735

2836
class Solution2(object):
2937
def moveZeroes(self, nums):
@@ -39,3 +47,9 @@ def moveZeroes(self, nums):
3947

4048
for i in xrange(pos, len(nums)):
4149
nums[i] = 0
50+
51+
52+
if __name__ == '__main__':
53+
s = Solution()
54+
r = s.moveZeroes([0, 1, 0, 3, 12])
55+
print r

Python/nim-game.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# the last stone will always be removed by your friend.
1717
#
1818

19+
1920
class Solution(object):
2021
def canWinNim(self, n):
2122
"""

Python/reverse-integer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919
# Throw an exception? Good, but what if throwing an exception is not an option?
2020
# You would then have to re-design the function (ie, add an extra parameter).
21-
#
21+
2222

2323
class Solution(object):
2424
def reverse(self, x):

Python/rotate-array.py

+29-12
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,58 @@
1010
#
1111

1212
class Solution:
13-
# @param nums, a list of integer
14-
# @param k, num of steps
15-
# @return nothing, please modify the nums list in-place.
13+
"""
14+
:type nums: List[int]
15+
:type k: int
16+
:rtype: void Do not return anything, modify nums in-place instead.
17+
"""
18+
1619
def rotate(self, nums, k):
1720
k %= len(nums)
1821
self.reverse(nums, 0, len(nums))
1922
self.reverse(nums, 0, k)
2023
self.reverse(nums, k, len(nums))
21-
24+
2225
def reverse(self, nums, start, end):
2326
while start < end:
24-
nums[start], nums[end-1] = nums[end-1], nums[start]
27+
nums[start], nums[end - 1] = nums[end - 1], nums[start]
2528
start += 1
2629
end -= 1
2730

31+
def rotate2(self, nums, k):
32+
"""
33+
:type nums: List[int]
34+
:type k: int
35+
:rtype: void Do not return anything, modify nums in-place instead.
36+
"""
37+
nums[:] = nums[len(nums) - k:] + nums[:len(nums) - k]
38+
39+
2840
from fractions import gcd
2941

42+
3043
class Solution2:
31-
# @param nums, a list of integer
32-
# @param k, num of steps
33-
# @return nothing, please modify the nums list in-place.
44+
"""
45+
:type nums: List[int]
46+
:type k: int
47+
:rtype: void Do not return anything, modify nums in-place instead.
48+
"""
49+
3450
def rotate(self, nums, k):
3551
k %= len(nums)
3652
num_cycles = gcd(len(nums), k)
3753
cycle_len = len(nums) / num_cycles
3854
for i in xrange(num_cycles):
3955
self.apply_cycle_permutation(k, i, cycle_len, nums)
40-
56+
4157
def apply_cycle_permutation(self, k, offset, cycle_len, nums):
4258
tmp = nums[offset]
4359
for i in xrange(1, cycle_len):
44-
nums[(offset+i*k) % len(nums)], tmp = tmp, nums[(offset+i*k) % len(nums)]
60+
nums[(offset + i * k) % len(nums)], tmp = tmp, nums[(offset + i * k) % len(nums)]
4561
nums[offset] = tmp
46-
62+
63+
4764
if __name__ == '__main__':
48-
nums = [1,2,3,4,5,6,7]
65+
nums = [1, 2, 3, 4, 5, 6, 7]
4966
Solution().rotate(nums, 3)
5067
print nums

Python/sum-of-two-integers.py

+47
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# Example:
88
# Given a = 1 and b = 2, return 3.
99

10+
1011
class Solution(object):
1112
def getSum(self, a, b):
1213
"""
@@ -28,3 +29,49 @@ def getSum(self, a, b):
2829
b = (b | ~mask) if (b & neg_bit) else (b & mask)
2930

3031
return a
32+
33+
def getSum2(self, a, b):
34+
"""
35+
:type a: int
36+
:type b: int
37+
:rtype: int
38+
"""
39+
# 32 bits integer max
40+
MAX = 0x7FFFFFFF
41+
# 32 bits interger min
42+
MIN = 0x80000000
43+
# mask to get last 32 bits
44+
mask = 0xFFFFFFFF
45+
while b:
46+
# ^ get different bits and & gets double 1s, << moves carry
47+
a, b = (a ^ b) & mask, ((a & b) << 1) & mask
48+
# if a is negative, get a's 32 bits complement positive first
49+
# then get 32-bit positive's Python complement negative
50+
return a if a <= MAX else ~(a ^ mask)
51+
52+
def minus(self, a, b):
53+
b = self.getSum(~b, 1)
54+
return self.getSum(a, b)
55+
56+
def multiply(self, a, b):
57+
isNeg = (a > 0) ^ (b > 0)
58+
x = a if a > 0 else self.getSum(~a, 1)
59+
y = b if b > 0 else self.getSum(~b, 1)
60+
ans = 0
61+
while y & 0x01:
62+
ans = self.getSum(ans, x)
63+
y >>= 1
64+
x <<= 1
65+
return self.getSum(~ans, 1) if isNeg else ans
66+
67+
def divide(self, a, b):
68+
isNeg = (a > 0) ^ (b > 0)
69+
x = a if a > 0 else self.getSum(~a, 1)
70+
y = b if b > 0 else self.getSum(~b, 1)
71+
ans = 0
72+
for i in range(31, -1, -1):
73+
if (x >> i) >= y:
74+
x = self.minus(x, y << i)
75+
ans = self.getSum(ans, 1 << i)
76+
return self.getSum(~ans, 1) if isNeg else ans
77+

0 commit comments

Comments
 (0)