📌  相关文章
📜  使用最多一次交换操作获得下一个更大的数字

📅  最后修改于: 2021-04-26 09:07:19             🧑  作者: Mango

给定一个非负数num 。问题是要通过在num中任意两个数字之间执行最多交换操作来找到大于num的最小数字。如果无法形成更大的数字,请打印“不可能”。

该数字可能非常大,甚至可能不适合long long int。

例子:

Input : num = "218765"
Output : 258761
We swap 5 and 1 to get the smallest
number greater than 'num'

Input : num = "541322"
Output : 542312

方法:首先找到最右边的数字的索引,该数字比它大且位于它的右侧。使其索引为ind 。现在,找到最小数字的索引,该索引大于索引ind处的数字,并在该数字的右边。使其索引为greatSmallDgt 。最后,在索引indgreatSmallDgt处交换数字。如果num的数字按降序排列,则打印“不可能”。

C++
// C++ implementation to find the next higher number
// using atmost one swap operation
#include 
  
using namespace std;
  
// function to find the next higher number
// using atmost one swap operation
string nxtHighUsingAtMostOneSwap(string num)
{
    int l = num.size();
      
    // to store the index of the largest digit
    // encountered so far from the right
    int posRMax = l-1;
      
    // to store the index of rightmost digit
    // which has a digit greater to it on its
    // right side
    int index = -1;
      
    // finding the 'index' of rightmost digit
    // which has a digit greater to it on its
    // right side
    for (int i=l-2; i>=0; i--)
    {
        if (num[i] >= num[posRMax])
            posRMax = i;
          
        // required digit found, store its 
        // 'index' and break    
        else
        {
            index = i;
            break;
        }    
    }
      
    // if no such digit is found which has a
    // larger digit on its right side
    if (index == -1)
        return "Not Possible";
      
    // to store the index of the smallest digit 
    // greater than the digit at 'index' and right
    // to it    
    int greatSmallDgt = -1;
      
    // finding the index of the smallest digit 
    // greater than the digit at 'index'
    // and right to it    
    for (int i=l-1; i>index; i--)    
    {
        if (num[i] > num[index])
        {
            if (greatSmallDgt == -1)
                greatSmallDgt = i;
            else if (num[i] <= num[greatSmallDgt])    
                greatSmallDgt = i;
        }
    }
      
    // swapping the digits
    char temp = num[index];
    num[index] = num[greatSmallDgt];
    num[greatSmallDgt] = temp;
      
    // required number
    return num;
}
  
// Driver program to test above
int main()
{
    string num = "218765";
    cout << "Original number: " << num << endl;
    cout << "Next higher number: "
        << nxtHighUsingAtMostOneSwap(num);
    return 0;
}


Java
// JAVA implementation to find the next higher
// number using atmost one swap operation
class GFG{
      
    // function to find the next higher number
    // using atmost one swap operation
    static String nextHighUsingAtMostOneSwap(String st)
    {
        char num[] = st.toCharArray();
        int l = num.length;
           
        // to store the index of the largest digit
        // encountered so far from the right
        int posRMax = l - 1;
           
        // to store the index of rightmost digit
        // which has a digit greater to it on its
        // right side
        int index = -1;
           
        // finding the 'index' of rightmost digit
        // which has a digit greater to it on its
        // right side
        for (int i = l - 2; i >= 0; i--)
        {
            if (num[i] >= num[posRMax])
                posRMax = i;
               
            // required digit found, store its 
            // 'index' and break    
            else
            {
                index = i;
                break;
            }    
        }
           
        // if no such digit is found which has a
        // larger digit on its right side
        if (index == -1)
            return "Not Possible";
           
        // to store the index of the smallest digit 
        // greater than the digit at 'index' and
        // right to it    
        int greatSmallDgt = -1;
           
        // finding the index of the smallest 
        // digit greater than the digit at 
        // 'index' and right to it    
        for (int i = l - 1; i > index; i--)    
        {
            if (num[i] > num[index])
            {
                if (greatSmallDgt == -1)
                    greatSmallDgt = i;
                else if (num[i] <= num[greatSmallDgt])    
                    greatSmallDgt = i;
            }
        }
           
        // swapping the digits
        char temp = num[index];
         num[index] = num[greatSmallDgt];
        num[greatSmallDgt] = temp;
           
        // required number
        return (String.valueOf(num));
    }
       
    // Driver program to test above
    public static void main(String[] args)
    {
        String num = "218765";
        System.out.println("Original number: " 
                           + num );
        System.out.println("Next higher number: "
              + nextHighUsingAtMostOneSwap(num));
    }
      
}
/*This code is contributed by Nikita Tiwari*/


Python
# Python implementation to find the next higher
# number using atmost one swap operation
  
# function to find the next higher number
# using atmost one swap operation
def nextHighUsingAtMostOneSwap(st) :
    num = list (st)
    l = len(num)
      
    # to store the index of the largest digit
    # encountered so far from the right
    posRMax = l - 1
           
    # to store the index of rightmost digit
    # which has a digit greater to it on its
    # right side
    index = -1
           
    # finding the 'index' of rightmost digit
    # which has a digit greater to it on its
    # right side
    i = l - 2 
    while i >= 0 :
        if (num[i] >= num[posRMax]) :
            posRMax = i
               
        # required digit found, store its 
        # 'index' and break    
        else :
            index = i
            break
        i = i - 1
           
    # if no such digit is found which has 
    # a larger digit on its right side
    if (index == -1) :
        return "Not Possible"
           
    # to store the index of the smallest digit 
    # greater than the digit at 'index' and 
    # right to it    
    greatSmallDgt = -1
           
    # finding the index of the smallest digit 
    # greater than the digit at 'index'
    # and right to it    
    i = l - 1
    while i > index :
        if (num[i] > num[index]) :
            if (greatSmallDgt == -1) :
                greatSmallDgt = i
            elif (num[i] <= num[greatSmallDgt]) :
                greatSmallDgt = i
              
        i = i - 1
           
    # swapping the digits
    temp = num[index]
    num[index] = num[greatSmallDgt];
    num[greatSmallDgt] = temp;
           
    # required number
    return ''.join(num)
      
      
# Driver program to test above
num = "218765"
print"Original number: " , num 
print "Next higher number: ", nextHighUsingAtMostOneSwap(num)
  
# This code is contributed by Nikita Tiwari.


C#
// C# implementation to find the 
// next higher number using atmost
// one swap operation
using System;
  
class GFG
{
  
// function to find the next 
// higher number using atmost 
// one swap operation
static String nextHighUsingAtMostOneSwap(String st)
{
    char[] num = st.ToCharArray();
    int l = num.Length;
      
    // to store the index of the 
    // largest digit encountered 
    // so far from the right
    int posRMax = l - 1;
      
    // to store the index of rightmost 
    // digit which has a digit greater
    // to it on its right side
    int index = -1;
      
    // finding the 'index' of rightmost
    // digit which has a digit greater 
    // to it on its right side
    for (int i = l - 2; i >= 0; i--)
    {
        if (num[i] >= num[posRMax])
            posRMax = i;
          
        // required digit found, store 
        // its 'index' and break 
        else
        {
            index = i;
            break;
        } 
    }
      
    // if no such digit is found 
    // which has a larger digit
    // on its right side
    if (index == -1)
        return "Not Possible";
      
    // to store the index of the 
    // smallest digit greater than 
    // the digit at 'index' and
    // right to it 
    int greatSmallDgt = -1;
      
    // finding the index of the  
    // smallest digit greater 
    // than the digit at 'index'
    // and right to it 
    for (int i = l - 1; i > index; i--) 
    {
        if (num[i] > num[index])
        {
            if (greatSmallDgt == -1)
                greatSmallDgt = i;
            else if (num[i] <= num[greatSmallDgt]) 
                greatSmallDgt = i;
        }
    }
      
    // swapping the digits
    char temp = num[index];
    num[index] = num[greatSmallDgt];
    num[greatSmallDgt] = temp;
      
    string res = new string(num);
      
    // required number
    return res;
}
  
// Driver Code
public static void Main()
{
    String num = "218765";
    Console.WriteLine("Original number: "
                                 + num );
    Console.WriteLine("Next higher number: "
         + nextHighUsingAtMostOneSwap(num));
}
}
  
// This code is contributed by mits


PHP
= 0; $i--)
    {
        if ($num[$i] >= $num[$posRMax])
            $posRMax = $i;
          
        // required digit 
        // found, store its 
        // 'index' and break 
        else
        {
            $index = $i;
            break;
        } 
    }
      
    // if no such digit is 
    // found which has a
    // larger digit on its 
    // right side
    if ($index == -1)
        return "Not Possible";
      
    // to store the index of 
    // the smallest digit 
    // greater than the digit
    // at 'index' and right
    // to it 
    $greatSmallDgt = -1;
      
    // finding the index of 
    // the smallest digit 
    // greater than the digit 
    // at 'index' and right to it 
    for ($i = $l - 1;
         $i > $index; $i--) 
    {
        if ($num[$i] > $num[$index])
        {
            if ($greatSmallDgt == -1)
                $greatSmallDgt = $i;
            else if ($num[$i] <= $num[$greatSmallDgt]) 
                $greatSmallDgt = $i;
        }
    }
      
    // swapping the digits
    $temp = $num[$index];
    $num[$index] = $num[$greatSmallDgt];
    $num[$greatSmallDgt] = $temp;
      
    // required number
    return $num;
}
  
// Driver Code
$num = "218765";
echo "Original number: ".$num."\n";
echo "Next higher number: ". 
      nxtHighUsingAtMostOneSwap($num);
  
// This code is contributed by mits
?>


输出:

Original number: 218765
Next higher number: 258761

时间复杂度: O(n),其中nnum中的位数。