Python教程

Leetcode 1881. Maximum Value after Insertion [Python]

本文主要是介绍Leetcode 1881. Maximum Value after Insertion [Python],对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

分+ - 数字,如果是+ 数字,则找到第一个比x小的数字,把x放到这个数字前面。如果是-数字,则从第1位开始遍历,找到第一个比x大的数字,把x放到这个数字前面。

class Solution:
    def maxValue(self, n: str, x: int) -> str:
        if n[0] != '-':
            for i in range(len(n)):
                if int(n[i]) < x:
                    return n[:i] + str(x) + n[i:]     
        else:
            for i in range(1, len(n)):
                if int(n[i]) > x:
                    return n[:i] + str(x) + n[i:]
        return n + str(x)
这篇关于Leetcode 1881. Maximum Value after Insertion [Python]的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!