📌  相关文章
📜  检查一个数字在给定的订单中是否有数字

📅  最后修改于: 2021-10-26 05:24:08             🧑  作者: Mango

给定一个数字 N。 任务是检查数字的数字是否遵循以下任何顺序:

  • 数字严格按照递增顺序排列。
  • 或者,数字严格按降序排列。
  • 或者,数字首先严格遵循递增顺序,然后严格遵循递减顺序。

如果数字遵循上述任何顺序,则打印 YES,否则打印 NO。
例子

Input : N = 415
Output : NO

Input : N = 123454321
Output : YES

通过逐个提取每个数字从右到左遍历数字。保留一个指针来判断当前序列是降序还是升序,-1表示严格升序,1表示严格降序。起初,当我们从右到左时,序列应该严格递增。当我们遇到一个小于前一个数字的数字时,将标志更改为递减(即 -1),而在按递增顺序我们得到任何等于前一个数字的数字时,我们直接打印 NO。
下面是上述方法的实现:

C++
// CPP program to check if the digits are in
// the given order
#include
using namespace std;
 
// Check if the digits follow the correct order
bool isCorrectOrder(int n)
{
    bool flag = true;
     
    // to store the previous digit
    int prev = -1;
     
    // pointer to tell what type of sequence
    // are we dealing with
    int type = -1;
 
    while(n != 0)
    {
        if(type ==-1)
        {
            if(prev ==-1)
            {
                prev = n % 10;
                n = n/10;
                continue;
            }
            // check if we have same digit
            // as the previous digit
            if(prev == n % 10)
            {  
                flag = false;
                break;
            }
            // checking the peak point of the number
            if(prev > n % 10)
            { 
                type = 1;
                prev = n % 10;
                n = n/10;
                continue;
            }
             
            prev = n % 10;
            n = n / 10;
        }
        else
        {  
            // check if we have same digit
            // as the previous digit
            if(prev == n % 10)
            {  
                flag = false;
                break;
            }
            // check if the digit is greater than
            // the previous one
            // If true, then break from the loop as
            // we are in descending order part
            if(prev < n % 10)
            {          
                flag = false;
                break;
            }
              
            prev = n % 10;
            n = n / 10;
        }
    }
 
    return flag;
}
 
// Driver code
int main()
{
    int n = 123454321;
     
    if(isCorrectOrder(n))
        cout<<"YES";
    else
        cout<<"NO";
     
    return 0;
}


Java
// Java program to check if the digits are in
// the given order
import java.io.*;
 
class GFG {
 
// Check if the digits follow the correct order
static boolean isCorrectOrder(int n)
{
    boolean flag = true;
     
    // to store the previous digit
    int prev = -1;
     
    // pointer to tell what type of sequence
    // are we dealing with
    int type = -1;
 
    while(n != 0)
    {
        if(type ==-1)
        {
            if(prev ==-1)
            {
                prev = n % 10;
                n = n/10;
                continue;
            }
            // check if we have same digit
            // as the previous digit
            if(prev == n % 10)
            {
                flag = false;
                break;
            }
            // checking the peak point of the number
            if(prev > n % 10)
            {
                type = 1;
                prev = n % 10;
                n = n/10;
                continue;
            }
             
            prev = n % 10;
            n = n / 10;
        }
        else
        {
            // check if we have same digit
            // as the previous digit
            if(prev == n % 10)
            {
                flag = false;
                break;
            }
            // check if the digit is greater than
            // the previous one
            // If true, then break from the loop as
            // we are in descending order part
            if(prev < n % 10)
            {        
                flag = false;
                break;
            }
             
            prev = n % 10;
            n = n / 10;
        }
    }
 
    return flag;
}
 
// Driver code
 
    public static void main (String[] args) {
        int n = 123454321;
     
    if(isCorrectOrder(n))
        System.out.println("YES");
    else
        System.out.println("NO");
    }
}
 
 // This code is contributed by ajit


Python3
# Python3 program to check if the
# digits are in the given order
 
# Check if the digits follow
# the correct order
def isCorrectOrder(n):
 
    flag = True;
     
    # to store the previous digit
    prev = -1;
     
    # pointer to tell what type of
    # sequence are we dealing with
    type = -1;
 
    while(n != 0):
        if(type ==-1):
            if(prev ==-1):
                prev = n % 10;
                n = int(n / 10);
                continue;
             
            # check if we have same digit
            # as the previous digit
            if(prev == n % 10):
                flag = False;
                break;
             
            # checking the peak point
            # of the number
            if(prev > n % 10):
                type = 1;
                prev = n % 10;
                n = int(n / 10);
                continue;
             
            prev = n % 10;
            n = int(n / 10);
        else:
             
            # check if we have same digit
            # as the previous digit
            if(prev == n % 10):
                flag = False;
                break;
             
            # check if the digit is greater
            # than the previous one
            # If true, then break from the
            # loop as we are in descending
            # order part
            if(prev < n % 10):
                flag = False;
                break;
             
            prev = n % 10;
            n = int(n / 10);
 
    return flag;
 
# Driver code
n = 123454321;
 
if(isCorrectOrder(n)):
    print("YES");
else:
    print("NO");
 
# This Code is contributed by mits


C#
// C# program to check if the
// digits are in the given order
using System;
 
class GFG
{
 
// Check if the digits follow
// the correct order
static bool isCorrectOrder(int n)
{
    bool flag = true;
     
    // to store the previous digit
    int prev = -1;
     
    // pointer to tell what type of
    // sequence are we dealing with
    int type = -1;
 
    while(n != 0)
    {
        if(type == -1)
        {
            if(prev == -1)
            {
                prev = n % 10;
                n = n / 10;
                continue;
            }
             
            // check if we have same digit
            // as the previous digit
            if(prev == n % 10)
            {
                flag = false;
                break;
            }
             
            // checking the peak point
            // of the number
            if(prev > n % 10)
            {
                type = 1;
                prev = n % 10;
                n = n / 10;
                continue;
            }
             
            prev = n % 10;
            n = n / 10;
        }
        else
        {
            // check if we have same digit
            // as the previous digit
            if(prev == n % 10)
            {
                flag = false;
                break;
            }
             
            // check if the digit is greater
            // than the previous one
            // If true, then break from the
            // loop as we are in descending
            // order part
            if(prev < n % 10)
            {    
                flag = false;
                break;
            }
             
            prev = n % 10;
            n = n / 10;
        }
    }
 
    return flag;
}
 
// Driver code
public static void Main ()
{
    int n = 123454321;
     
    if(isCorrectOrder(n))
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
}
 
// This code is contributed by Shashank


PHP
 $n % 10)
            {
                $type = 1;
                $prev = $n % 10;
                $n = (int)$n / 10;
                continue;
            }
             
            $prev = $n % 10;
            $n = (int)$n / 10;
        }
        else
        {
            // check if we have same digit
            // as the previous digit
            if($prev == $n % 10)
            {
                $flag = false;
                break;
            }
             
            // check if the digit is greater
            // than the previous one
            // If true, then break from the
            // loop as we are in descending
            // order part
            if($prev < $n % 10)
            {    
                $flag = false;
                break;
            }
             
            $prev = $n % 10;
            $n = (int) $n / 10;
        }
    }
 
    return $flag;
}
 
// Driver code
$n = 123454321;
 
if(isCorrectOrder($n))
    echo "YES";
else
    echo "NO";
 
// This Code is contributed by ajit
?>


Javascript


输出:

YES

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程