From 6d6d9306c91ae67ca6c6b90c5121cb74d936a1de Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 11 Jan 2017 16:27:21 +0800 Subject: [PATCH] add some solutions --- Python/house-robber.py | 11 ++++++++ Python/number-complement.py | 32 ++++++++++++++++++++++++ Python/number-of-segments-in-a-string.py | 8 ++++++ Python/power-of-four.py | 10 ++++++++ Python/ransom-note.py | 4 +-- 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 Python/number-complement.py diff --git a/Python/house-robber.py b/Python/house-robber.py index db2379811..3efd6e8a0 100644 --- a/Python/house-robber.py +++ b/Python/house-robber.py @@ -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]) diff --git a/Python/number-complement.py b/Python/number-complement.py new file mode 100644 index 000000000..0adb83481 --- /dev/null +++ b/Python/number-complement.py @@ -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 diff --git a/Python/number-of-segments-in-a-string.py b/Python/number-of-segments-in-a-string.py index 066b98208..1a5c0086f 100644 --- a/Python/number-of-segments-in-a-string.py +++ b/Python/number-of-segments-in-a-string.py @@ -13,6 +13,7 @@ # Input: "Hello, my name is John" # Output: 5 + class Solution(object): def countSegments(self, s): """ @@ -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]) diff --git a/Python/power-of-four.py b/Python/power-of-four.py index b8da27b99..74ca8217a 100644 --- a/Python/power-of-four.py +++ b/Python/power-of-four.py @@ -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 diff --git a/Python/ransom-note.py b/Python/ransom-note.py index 347cc0548..917334555 100644 --- a/Python/ransom-note.py +++ b/Python/ransom-note.py @@ -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): @@ -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)