LeetCode 刷题记录(6-10题)

 

6 Z 字形变换(题目链接

class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        lists = []
        for i in range(numRows):
            lists.append('')
        numMids = numRows - 2
        if numMids < 0:
            numMids = 0
        for i_s in range(len(s)):
            rest = i_s % (numMids + numRows)
            if rest < numRows:
                lists[rest] = ''.join([lists[rest], s[i_s]])
            else:
                lists[numMids + numRows - rest] = ''.join([lists[numMids + numRows - rest], s[i_s]])
        s_result = ''.join(lists)
        return s_result

 

7.整数反转(题目链接

class Solution:
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        y = 0
        z = 1    
        if x < 0:
            z = -1
            x = -x      
        while x != 0:
            item = x % 10
            x = x // 10
            if 2147483647 - 10 * y <= item:
                return 0
            y = y * 10 + item
        return y*z

 

8.字符串转换整数 (atoi)(题目链接

class Solution:
    def myAtoi(self, str: 'str') -> 'int':
        sign = 0
        num = 0
        for i in str:
            if sign == 0 and i == ' ':
                continue
            elif sign == 0 and i == '-':
                sign = -1
            elif sign == 0 and i == '+':
                sign = 1
            elif i in ['0','1','2','3','4','5','6','7','8','9']:
                if (sign == 1 or sign == 0) and (2147483647 - int(i)) // 10 < num:
                    return 2147483647
                elif sign == -1 and (2147483648 - int(i)) // 10 < num:
                    return -2147483648 
                else:
                    if sign == 0:
                        sign = 1
                    flag = 1
                    num = num * 10 + int(i)
            else:
                break
        if sign == 0:
            return num
        return sign * num

 

转载于:https://www.cnblogs.com/weswes/p/10318816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值