Leetcode 算法题22

172Factorial Trailing Zeroes

求一个数阶乘的尾数为0的个数

我的代码:差不多想到这个思路就是对的

class Solution:
    def trailingZeroes(self, n):
        """
        :type n: int
        :rtype: int
        """
        ans = 0
        while n > 4:
            n = n // 5
            ans += n
        return ans


38.   Count and Say

The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2.     11
3.     21
4.     1211
5.     111221

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Example 1:

Input: 1
Output: "1"

Example 2:

Input: 4
Output: "1211"
我的代码:

class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        def say(s):
            last = s[0]
            count = 1
            ans = ''
            for i in s[1:]+' ':
                if i == last:
                    count += 1
                else :
                    ans = ans + str(count) + last
                    last = i
                    count = 1
            return ans
        resp = '1'
        for i in range(n-1):
            resp = say(resp)
        return resp
大神的代码:

Solution 1 … using a regular expression

def countAndSay(self, n):
    s = '1'
    for _ in range(n - 1):
        s = re.sub(r'(.)\\1*', lambda m: str(len(m.group(0))) + m.group(1), s)
    return s

Solution 2 … using a regular expression

def countAndSay(self, n):
    s = '1'
    for _ in range(n - 1):
        s = ''.join(str(len(group)) + digit
                    for group, digit in re.findall(r'((.)\\2*)', s))
    return s

Solution 3 … using groupby

def countAndSay(self, n):
    s = '1'
    for _ in range(n - 1):
        s = ''.join(str(len(list(group))) + digit
                    for digit, group in itertools.groupby(s))
    return s

342Power of Four

输入一个数,判断其是否为4的指数,不要用循环递归

我的代码:一般这种题就和转化二进制后的操作有关

def isPowerOfFour(self, num):  
        """ 
        :type num: int 
        :rtype: bool 
        """  
        return bool(re.match(r'^0b1(00)*$',bin(num)))


119Pascal's Triangle II

返回杨辉三角的第k行

For example, given k = 3,
Return [1,3,3,1].

我的代码:

 def getRow(self, rowIndex):  
        """ 
        :type rowIndex: int 
        :rtype: List[int] 
        """  
        ans = [1]  
        row = 0  
        while row != rowIndex:  
            ans = list(map(lambda x,y:x+y,[0]+ans,ans+[0]))  
            row += 1  
        return ans

discuss里一个更简洁的版本:我不记得为啥我要用while了

class Solution(object):  
    def getRow(self, rowIndex):  
        """ 
        :type rowIndex: int 
        :rtype: List[int] 
        """  
        row = [1]  
        for _ in range(rowIndex):  
            row = [x + y for x, y in zip([0]+row, row+[0])]  
        return row  

232 Implement Queue using Stacks

这道题在python语言里完全没难度

class MyQueue(object):  
  
    def __init__(self):  
        """ 
        Initialize your data structure here. 
        """  
        self.list = []  
  
    def push(self, x):  
        """ 
        Push element x to the back of queue. 
        :type x: int 
        :rtype: void 
        """  
        self.list.append(x)  
  
    def pop(self):  
        """ 
        Removes the element from in front of queue and returns that element. 
        :rtype: int 
        """  
        a = self.list.pop(0)  
        return a  
      
    def peek(self):  
        """ 
        Get the front element. 
        :rtype: int 
        """  
        return self.list[0]  
  
    def empty(self):  
        """ 
        Returns whether the queue is empty. 
        :rtype: bool 
        """  
        return self.list == []  
  
  
# Your MyQueue object will be instantiated and called as such:  
# obj = MyQueue()  
# obj.push(x)  
# param_2 = obj.pop()  
# param_3 = obj.peek()  
# param_4 = obj.empty()

225 Implement Stack using Queues

和上道题一样,不知道意义何在

class MyStack:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.list = []
        

    def push(self, x):
        """
        Push element x onto stack.
        :type x: int
        :rtype: void
        """
        self.list.append(x)

    def pop(self):
        """
        Removes the element on top of the stack and returns that element.
        :rtype: int
        """
        a = self.list.pop()
        return a

    def top(self):
        """
        Get the top element.
        :rtype: int
        """
        return self.list[-1]

    def empty(self):
        """
        Returns whether the stack is empty.
        :rtype: bool
        """
        return self.list == []


# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()





225 Implement Stack using Queues

你可能感兴趣的:(Leetcode 算法题22)