Leetcode

Leetcode 1881. Maximum Value aft

2021-09-24  本文已影响0人  SnailTyan

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Maximum Value after Insertion

2. Solution

解析:Version 1,如果n是正数,则x应该位于从左向右找第一个比其小的数字所在的位置,如果是负数,x应该位于从左往右找第一个比其大的数字所在的位置。

class Solution:
    def maxValue(self, n: str, x: int) -> str:
        result = ''
        if n[0] == '-':
            i = 0
            while i < len(n) and n[i] <= str(x):
                i += 1
            result = n[:i] + str(x) + n[i:]
        else:
            i = 0
            while i < len(n) and n[i] >= str(x):
                i += 1
            result = n[:i] + str(x) + n[i:]
        return result

Reference

  1. https://leetcode.com/problems/maximum-value-after-insertion/
上一篇 下一篇

猜你喜欢

热点阅读