leetcode66加一(python实现)

1.问题描述


给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

示例 1:

输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。
示例 2:

输入: [4,3,2,1]
输出: [4,3,2,2]
解释: 输入数组表示数字 4321。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/plus-one

2.python求解


 1.我的思路,把list数组中每个元素乘以10的N次方的形式累加,转为int。对于Int+1再转为list

class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        l = len(digits)
        r_sum = 0
        for i in range(l):
            r_sum += digits[i]*pow(10,l-i-1)
        return map(int,str(r_sum+1))

 2.其他思路,逆序处理List,

首先对最后一个数值元素+1。

  • 如果+1后小于10,则直接返回digits
  • 如果加1后等于10,则置为0,下面分两种情况:
    • 如果这个元素是第一个元素,则需要往前补一个1
    • 如果这个元素不是第一个元素,就置为0后,往前的一个元素+1;
class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        for i in range(len(digits)-1,-1,-1):
            digits[i] += 1
            if digits[i]==10:
                digits[i] = 0
                if i==0:
                    digits.insert(0,1)
            else:
                break
        return digits

3.python知识点补充


 1.关于N次幂

  • 表示10的N次幂,可以用e来表示
>>> 1e5
100000.0
>>> 2e5
200000.0
>>> 2e-5
2e-05
>>> 2e-1
0.2

  • pow(x,y)返回 xy(x的y次方) 的值。好像不用import math耶
>>> pow(2,5)
32
>>> pow(2,1/2)
1.4142135623730951
>>> pow(10,3)
1000
  • map函数,

    map() 会根据提供的函数对指定序列做映射。

    第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。


map(function, iterable, ...)

    Return an iterator that applies function to every item of iterable, 
yielding the results. If additional iterable arguments are passed, 
function must take that many arguments and is applied to the items from all iterables in parallel.
 With multiple iterables, the iterator stops when the shortest iterable is exhausted. 

For cases where the function inputs are already arranged into argument tuples, 
see itertools.starmap().

>>>def square(x) :            # 计算平方数
...     return x ** 2
... 
>>> map(square, [1,2,3,4,5])   # 计算列表各个元素的平方
[1, 4, 9, 16, 25]
>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]
 
# 提供了两个列表,对相同位置的列表数据进行相加
>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
[3, 7, 11, 15, 19]

这里用map函数把int类型的一个整数,转为List
# num = 12345
# num_list = [1,2,3,4,5]
num_list = map(int,str(num))
  • range函数,着重负数形式,这里就不用把list逆序了,只需要逆序的索引来遍历digits
两种形式
1.range(N)得到[0,N-1],即[0,N),以1为步长,左闭右包
2.range(start,stop,step),得到在[start,stop)以step为步长的list,也是左闭右包哦
4.range(start,stop)得到在[start,stop)以1为步长的list,也是左闭右包哦

几个栗子

>>>range(10)        # 从 0 开始到 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11)     # 从 1 开始到 11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5)  # 步长为 5
[0, 5, 10, 15, 20, 25]
>>> range(0, -10, -1) # 负数
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

 


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值