📌  相关文章
📜  查找通过插入给定数字形成的最小数字

📅  最后修改于: 2022-05-13 01:56:09.588000             🧑  作者: Mango

查找通过插入给定数字形成的最小数字

给定一个字符串N和一个数字X ([1, 9]) 任务是找到通过在N中的任意位置插入数字X形成的最小整数。

例子:

朴素方法:解决此问题的一种简单方法是在所有位置(负号左侧除外,如果存在)插入X ,并在所有形成的数字中找到最小值。该方法在较大字符串的情况下效率低下

有效方法:主要思想是,如果N是正插入,则形成的数字是最小的,而如果N是负数,则插入X ,例如形成的数字是最大的,忽略负号。按照以下步骤优化上述方法:

  • 初始化两个变量,比如len =字符串N 的长度position = n + 1
  • 如果N为负( N[0] = '-'),则从第 (n-1)索引到第 1索引遍历字符串并检查N[i] – '0' < X,如果为真则更新位置 =我
  • 如果N为正则从第 (n-1)索引遍历字符串到第0索引并检查N[i] – '0' > X,如果为真则更新position = i
  • N的索引位置插入X
  • 最后,返回字符串N。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to insert X in N and
// return the minimum value string
string MinValue(string N, int X)
{
 
    // Variable to store length
    // of string N
    int len = N.size();
 
    // Variable to denote the position
    // where X must be added
    int position = len + 1;
 
    // If the given string N represent
    // a negative value
    if (N[0] == '-') {
        // X must be place at the last
        // index where is greater than N[i]
        for (int i = len - 1; i >= 1; i--) {
            if ((N[i] - '0') < X) {
                position = i;
            }
        }
    }
    else {
        // For positive numbers, X must be
        // placed at the last index where
        // it is smaller than N[i]
        for (int i = len - 1; i >= 0; i--) {
            if ((N[i] - '0') > X) {
                position = i;
            }
        }
    }
    // Insert X at that position
    N.insert(N.begin() + position, X + '0');
 
    // Return the string
    return N;
}
 
// Driver Code
int main()
{
    // Given Input
    string N = "89";
    int X = 1;
 
    // Function Call
    cout << MinValue(N, X) << "\n";
}


Java
// Java implementation of above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
 
  // Function to insert X in N and
  // return the minimum value string
  static String MinValue(String number, int x)
  {
 
    // Variable to store length
    // of string N
    int length = number.length();
 
    // Variable to denote the position
    // where X must be added
    int position = length + 1;
 
    // If the given string N represent
    // a negative value
    if (number.charAt(0) == '-') {
 
      // X must be place at the last
      // index where is greater than N[i]
      for (int i = number.length() - 1; i >= 1; --i) {
        if ((number.charAt(i) - 48) < x) {
          position = i;
        }
      }
    }
    else {
 
      // For positive numbers, X must be
      // placed at the last index where
      // it is smaller than N[i]
      for (int i = number.length() - 1; i >= 0; --i) {
        if ((number.charAt(i) - 48) > x) {
          position = i;
        }
      }
    }
 
    // Insert X at that position
    number
      = number.substring(0, position) + x
      + number.substring(position, number.length());
 
    // return string
    return number.toString();
  }
 
  // Driver call
  public static void main(String[] args)
  {
 
    // given input
    String number = "89";
    int x = 1;
 
    // function call
    System.out.print(MinValue(number, x));
  }
}
 
// This code is contributed by zack_aayush.


Python3
# Python Program for the above approach
 
# Function to insert X in N and
# return the minimum value string
def MinValue(N, X):
 
    # Variable to store length
    # of string N
    N = list(N);
    ln = len(N)
 
    # Variable to denote the position
    # where X must be added
    position = ln + 1
 
    # If the given string N represent
    # a negative value
    if (N[0] == '-'):
       
        # X must be place at the last
        # index where is greater than N[i]
        for i in range(ln - 1, 0, -1):
            if ((ord(N[i]) - ord('0')) < X):
                position = i
 
    else:
        # For positive numbers, X must be
        # placed at the last index where
        # it is smaller than N[i]
        for i in range(ln - 1, -1, -1):
            if ((ord(N[i]) - ord('0')) > X):
                position = i
 
    # Insert X at that position
    c = chr(X + ord('0'))
 
    str = N.insert(position, c);
 
 
    # Return the string
    return ''.join(N)
 
# Driver Code
 
# Given Input
N = "89"
X = 1
 
# Function Call
print(MinValue(N, X))
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
 
class GFG {
     
  // Function to insert X in N and
  // return the minimum value string
  static String MinValue(string number, int x)
  {
 
    // Variable to store length
    // of string N
    int length = number.Length;
 
    // Variable to denote the position
    // where X must be added
    int position = length + 1;
 
    // If the given string N represent
    // a negative value
    if (number[0] == '-') {
 
      // X must be place at the last
      // index where is greater than N[i]
      for (int i = number.Length - 1; i >= 1; --i) {
        if ((number[i] - 48) < x) {
          position = i;
        }
      }
    }
    else {
 
      // For positive numbers, X must be
      // placed at the last index where
      // it is smaller than N[i]
      for (int i = number.Length - 1; i >= 0; --i) {
        if ((number[i] - 48) > x) {
          position = i;
        }
      }
    }
 
    // Insert X at that position
    number
      = number.Substring(0, position) + x
      + number.Substring(position, number.Length);
 
    // return string
    return number.ToString();
  }
     
    // Driver code
    public static void Main()
    {
        // given input
    string number = "89";
    int x = 1;
 
    // function call
    Console.WriteLine(MinValue(number, x));
    }
}
 
// This code is contributed by avijitmondal1998.


Javascript


输出
189

时间复杂度: O(N)
辅助空间: O(1)