Leetcode 刷题必须Review 十 Lintcode(145 23 1141 768 485 )

145 · 大小写转换

将一个字符由小写字母转换为大写字母
在这里插入图片描述

def lowercaseToUppercase(self, character):
        return character.upper()

之前写的方法:

def lowercaseToUppercase(self, character):
	return ord(character) + ord('A') - ord('a')
def lowercaseToUppercase(self, character):
        # Write your code here
        #ASCII码中小写字母与对应的大写字母相差32
        return chr(ord(character) - 32)

23 · 判断数字与字母字符

给出一个字符c,如果它是一个数字或字母,返回true,否则返回false。
在这里插入图片描述

 def isAlphanumeric(self, c: str) -> bool:
        # write your code here
        return c.isalnum()

之前写的:

def isAlphanumeric(self, c: str) -> bool:
        # write your code here
        if ord(c) >= ord('0') and ord(c) <= ord('9'): return True
        elif ord(c) >= ord('a') and ord(c) <= ord('z'): return True 
        elif ord(c) >= ord('A') and ord(c) <= ord('Z'): return True
        else: return False

1141 · 月份天数

给定年份和月份,返回这个月的天数。

在这里插入图片描述

def getTheMonthDays(self, year, month):
        # write your code here
        months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        if month != 2:
            return months[month - 1]
        else:
            if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
                return months[month - 1] + 1
            else:
                return months[month - 1]

768 · 杨辉三角

给一整数 n, 返回杨辉三角的前 n 行
在这里插入图片描述

def calcYangHuisTriangle(self, n):
        # write your code here
        res = [[1], [1, 1]]
        if n == 1: 
            return [res[0]]
        elif n == 2:
            return res
        elif n == 0:
            return []
        layer = 2
        while n != layer:
            tmp = [1]
            for i in range(1, layer):
                tmp.append(res[layer -1][i - 1] + res[layer -1][i])
            tmp.append(1)
            res.append(tmp)
            layer += 1
        return res

之前写的比这次写得好:

    def calcYangHuisTriangle(self, n):
        # write your code here
        if n <= 0: return []
        elif n == 1: return [[1]]
        elif n == 2: return [[1], [1, 1]]
        
        li = [[1], [1, 1]]
        for i in range(2, n):
            temp = [1]
            for j in range(1, i):
                temp.append(li[i-1][j-1] + li[i-1][j])
            temp.append(1)
            li.append(temp)
        return li

485 · 生成给定大小的数组

给你一个大小size,生成一个元素从1 到 size的数组

在这里插入图片描述

def generate(self, size):
        # write your code here
        return list(range(1, size + 1))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值