python Lintcode 刷题(一)

按难度或者通过率排序刷题
python Lintcode 刷题(一)_第1张图片
我的代码如下:

class Solution:
    # @param n: an integer
    # @return an integer f(n)
    def fibonacci(self, n):
        # write your code here
        a = 0
        b = 1
        for i in range(n-1):
            a, b = b, a+b
        return a

python Lintcode 刷题(一)_第2张图片
我的代码如下:

class Solution:
    """
    @param n: An integer as description
    @return: A list of strings.
    For example, if n = 7, your code should return
        ["1", "2", "fizz", "4", "buzz", "fizz", "7"]
    """
    def fizzBuzz(self, n):
        results = []
        for i in range(1, n+1):
            if i % 15 == 0:
                results.append("fizz buzz")
            elif i % 5 == 0:
                results.append("buzz")
            elif i % 3 == 0:
                results.append("fizz")
            else:
                results.append(str(i))
        return results

python Lintcode 刷题(一)_第3张图片

class Solution:
    # @param s: a string
    # @return: a boolean
    def isUnique(self, str):
        # write your code here
        results = []
        new_results = []
        for i in str:          #字符串变成列表
            results.append(i)
        for i in str:          #字符串变成去重的列表
            if i not in new_results:
                new_results.append(i)
        if results == new_results:#两个比较
            return True
        else:
            return False

你可能感兴趣的:(笔试,面试,刷题,概念)