Skip to content

Commit 6d6d930

Browse files
author
Jack
committed
add some solutions
1 parent 89fb7f3 commit 6d6d930

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

Python/house-robber.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,16 @@ def rob(self, num):
2626

2727
return num_i
2828

29+
def rob2(self, nums):
30+
"""
31+
:type nums: List[int]
32+
:rtype: int
33+
"""
34+
last, now = 0, 0
35+
for i in nums:
36+
last, now = now, max(last + i, now)
37+
return now
38+
39+
2940
if __name__ == '__main__':
3041
print Solution().rob([8,4,8,5,9,6,5,4,4,10])

Python/number-complement.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
4+
#
5+
# Note:
6+
# The given integer is guaranteed to fit within the range of a 32-bit signed integer.
7+
# You could assume no leading zero bit in the integer’s binary representation.
8+
# Example 1:
9+
# Input: 5
10+
# Output: 2
11+
# Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
12+
# Example 2:
13+
# Input: 1
14+
# Output: 0
15+
# Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
16+
17+
18+
class Solution(object):
19+
def findComplement(self, num):
20+
"""
21+
:type num: int
22+
:rtype: int
23+
"""
24+
return 2 ** (len(bin(num)) - 2) - 1 - num
25+
26+
27+
class Solution2(object):
28+
def findComplement(self, num):
29+
i = 1
30+
while i <= num:
31+
i <<= 1
32+
return (i - 1) ^ num

Python/number-of-segments-in-a-string.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# Input: "Hello, my name is John"
1414
# Output: 5
1515

16+
1617
class Solution(object):
1718
def countSegments(self, s):
1819
"""
@@ -24,3 +25,10 @@ def countSegments(self, s):
2425
if s[i] == ' ' and s[i-1] != ' ':
2526
result += 1
2627
return result
28+
29+
def countSegments2(self, s):
30+
"""
31+
:type s: str
32+
:rtype: int
33+
"""
34+
return len([i for i in s.strip().split(' ') if i])

Python/power-of-four.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,13 @@ def isPowerOfFour(self, num):
2929
while num and not (num & 0b11):
3030
num >>= 2
3131
return (num == 1)
32+
33+
34+
class Solution3(object):
35+
def isPowerOfFour(self, num):
36+
"""
37+
:type num: int
38+
:rtype: bool
39+
"""
40+
num = bin(num)
41+
return True if num[2:].startswith('1') and len(num[2:]) == num.count('0') and num.count('0') % 2 and '-' not in num else False

Python/ransom-note.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def canConstruct(self, ransomNote, magazine):
4141

4242
# Time: O(n)
4343
# Space: O(1)
44-
from collections import Counter
44+
import collections
4545

4646
class Solution2(object):
4747
def canConstruct(self, ransomNote, magazine):
@@ -50,4 +50,4 @@ def canConstruct(self, ransomNote, magazine):
5050
:type magazine: str
5151
:rtype: bool
5252
"""
53-
return not Counter(ransomNote) - Counter(magazine)
53+
return not collections.Counter(ransomNote) - collections.Counter(magazine)

0 commit comments

Comments
 (0)