Number Complement

问题描述

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.

求一个32位无符号数的二进制的反数 比如5 101 输出010 2

解决方法:

代码:

class Solution(object):

    def findComplement(self, num):

        """

        :type num: int

        :rtype: int

        """

        n = int(math.log(num,2)) + 1

        m = 2**n

        return (m-1) ^ num

class Solution(object):

    def findComplement(self, num):

        """

        :type num: int

        :rtype: int

        """

        i = 1

        while i <= num:

            i = i << 1

        return (i - 1) ^ num

方法:101 与 111异或后为010 10001 与 11111 异或后为01110 与相同位数的全部为1的数异或即可得到对应的反数。该问题变为如何找到与原数位数相同,且每一位都为1的数。

方法一,找出原数的位数,相同位数最大数就是要找的数

如5 101 3位,3位最大数为2**3-1 7(111)

方法二,1移位得出

如5 1<<1 10(2) 10<<1 100(4) 100<1 1000(8) 减去1即为111

Keyboard Row

Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below. Example 1: Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["Alaska", "Dad"] Note: You may use one character in the keyboard more than once. You may assume the input string will only contain letters of alphabet.

输入字符列表,找出字符串中字符只在键盘中一行的字符串。

解决方法:

遍历字符串列表,然后遍历每一行字符,判断每个字符串的字符是否在同一行,如果是保存。

代码:

def findWord(words):
    row1="QqWwEeRrTtYyUuIiOoPp"
    row2="AaSsDdFfGgHhJjKkLl"
    row3="ZzXxCcVvBbNnMm"
    rowlist=(row1, row2, row3)
    List=[]
    for word in words:
        for row in rowlist:
            Type="T"
            for i in word:
                if i not in row:
                    Type="F"
                    break
            if Type == "T":
                List.append(word)
                break
    return List

Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:

n = 15,

Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ]

输入一个数字,例如15,当为仅为3的倍数时输出Fizz,仅为5的倍数时为Buzz,当同时为3和5的倍数时为FizzBuzz

解决方法:

区别处理3种情况,3的倍数 5的倍数 15的倍数

代码:

class Solution(object):

    def fizzBuzz(self, n):

        """

        :type n: int

        :rtype: List[str]

        """

        if n <= 0:

            return False

        if n == 1:

            return ["1"]

        if n == 2:

            return ["1","2"]

        finallist = []

        if n >=3:

            for i in range(n+1):

                if i == 0:

                    pass

                elif i % 15 == 0:

                    finallist.append("FizzBuzz")

                elif i % 3 == 0:

                    finallist.append("Fizz")

                elif i % 5 == 0:

                    finallist.append("Buzz")

                else:

                    finallist.append(str(i))

            return finallist

by 李鹏