Skip to content

Commit

Permalink
add some solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack committed Jan 11, 2017
1 parent 89fb7f3 commit 6d6d930
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Python/house-robber.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,16 @@ def rob(self, num):

return num_i

def rob2(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
last, now = 0, 0
for i in nums:
last, now = now, max(last + i, now)
return now


if __name__ == '__main__':
print Solution().rob([8,4,8,5,9,6,5,4,4,10])
32 changes: 32 additions & 0 deletions Python/number-complement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
#
# Note:
# The given integer is guaranteed to fit within the range of a 32-bit signed integer.
# You could assume no leading zero bit in the integer’s binary representation.
# Example 1:
# Input: 5
# Output: 2
# Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
# Example 2:
# Input: 1
# Output: 0
# Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.


class Solution(object):
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
return 2 ** (len(bin(num)) - 2) - 1 - num


class Solution2(object):
def findComplement(self, num):
i = 1
while i <= num:
i <<= 1
return (i - 1) ^ num
8 changes: 8 additions & 0 deletions Python/number-of-segments-in-a-string.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# Input: "Hello, my name is John"
# Output: 5


class Solution(object):
def countSegments(self, s):
"""
Expand All @@ -24,3 +25,10 @@ def countSegments(self, s):
if s[i] == ' ' and s[i-1] != ' ':
result += 1
return result

def countSegments2(self, s):
"""
:type s: str
:rtype: int
"""
return len([i for i in s.strip().split(' ') if i])
10 changes: 10 additions & 0 deletions Python/power-of-four.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ def isPowerOfFour(self, num):
while num and not (num & 0b11):
num >>= 2
return (num == 1)


class Solution3(object):
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
num = bin(num)
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
4 changes: 2 additions & 2 deletions Python/ransom-note.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def canConstruct(self, ransomNote, magazine):

# Time: O(n)
# Space: O(1)
from collections import Counter
import collections

class Solution2(object):
def canConstruct(self, ransomNote, magazine):
Expand All @@ -50,4 +50,4 @@ def canConstruct(self, ransomNote, magazine):
:type magazine: str
:rtype: bool
"""
return not Counter(ransomNote) - Counter(magazine)
return not collections.Counter(ransomNote) - collections.Counter(magazine)

0 comments on commit 6d6d930

Please sign in to comment.