📌  相关文章
📜  生成按字典顺序最多N的所有数字

📅  最后修改于: 2021-04-22 09:01:20             🧑  作者: Mango

给定一个整数N ,任务是按字典顺序打印所有数字,直到N。

例子:

方法:
为了解决问题,请按照以下步骤操作:

  • 从1迭代到N,并以字符串形式存储所有数字。
  • 对包含字符串的向量进行排序。

下面是上述方法的实现:

C++
// C++ Program to implement the
// above approach
#include 
using namespace std;
 
// Function to print all the
// numbers up to n in
// lexicographical order
void lexNumbers(int n)
{
    vector s;
 
    for (int i = 1; i <= n; i++) {
        s.push_back(to_string(i));
    }
 
    sort(s.begin(), s.end());
    vector ans;
    for (int i = 0; i < n; i++)
        ans.push_back(stoi(s[i]));
 
    for (int i = 0; i < n; i++)
        cout << ans[i] << " ";
}
// Driver Program
int main()
{
 
    int n = 15;
    lexNumbers(n);
    return 0;
}


Java
// Java Program to implement the
// above approach
import java.util.*;
class GFG{
 
// Function to print all the
// numbers up to n in
// lexicographical order
static void lexNumbers(int n)
{
    Vector s = new Vector();
 
    for (int i = 1; i <= n; i++)
    {
        s.add(String.valueOf(i));
    }
 
    Collections.sort(s);
    Vector ans = new Vector();
    for (int i = 0; i < n; i++)
        ans.add(Integer.valueOf(s.get(i)));
 
    for (int i = 0; i < n; i++)
        System.out.print(ans.get(i) + " ");
}
// Driver Program
public static void main(String[] args)
{
    int n = 15;
    lexNumbers(n);
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program to implement
# above approach
 
# Function to print all the
# numbers up to n in
# lexicographical order
def lexNumbers(n):
     
    s = []
    for i in range(1, n + 1):
        s.append(str(i))
         
    s.sort()
    ans = []
     
    for i in range(n):
        ans.append(int(s[i]))
 
    for i in range(n):
        print(ans[i], end = ' ')
         
# Driver Code
if __name__ == "__main__":
     
    n = 15
    lexNumbers(n)
     
# This code is contributed by Ediga_Manisha


C#
// C# program to implement the
// above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to print all the
// numbers up to n in
// lexicographical order
static void lexNumbers(int n)
{
    List s = new List();
 
    for(int i = 1; i <= n; i++)
    {
       s.Add(String.Join("", i));
    }
 
    s.Sort();
    List ans = new List();
     
    for(int i = 0; i < n; i++)
       ans.Add(Int32.Parse(s[i]));
 
    for(int i = 0; i < n; i++)
       Console.Write(ans[i] + " ");
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 15;
     
    lexNumbers(n);
}
}
 
// This code is contributed by Rajput-Ji


C++
// C++ program to implement the
// above approach
#include 
using namespace std;
 
// Function to print all the
// numbers form l to r in
// lexicographical order
void lexNumbers(int l, int r)
{
    vector s;
 
    for(int i = l; i <= r; i++)
    {
        s.push_back(to_string(i));
    }
 
    sort(s.begin(),s.end());
    vector ans;
     
    for(int i = 0; i < s.size(); i++)
        ans.push_back(stoi(s[i]));
 
    for(int i = 0; i < s.size(); i++)
        cout << ans[i] << " ";
}
     
// Driver code
int main()
{
    int l = 9;
    int r = 21;
     
    lexNumbers(l, r);
}
 
// This code is contributed by ajaykr00kj


Java
// Java Program to implement the
// above approach
import java.util.*;
class GFG {
 
    // Function to print all the
    // numbers form l to r in
    // lexicographical order
    static void lexNumbers(int l, int r)
    {
        Vector s = new Vector();
 
        for (int i = l; i <= r; i++) {
            s.add(String.valueOf(i));
        }
 
        Collections.sort(s);
        Vector ans = new Vector();
        for (int i = 0; i < s.size(); i++)
            ans.add(Integer.valueOf(s.get(i)));
 
        for (int i = 0; i < s.size(); i++)
            System.out.print(ans.get(i) + " ");
    }
    // Driver Program
    public static void main(String[] args)
    {
        int l = 9;
        int r = 21;
        lexNumbers(l, r);
    }
}


Python3
# Python 3 program to implement
# the above approach
 
# Function to print all the
# numbers form l to r in
# lexicographical order
def lexNumbers(l, r):
 
    s = []
 
    for i in range(l, r + 1):
        s.append(str(i))
 
    s.sort()
    ans = []
 
    for i in range(len(s)):
        ans.append(int(s[i]))
 
    for i in range(len(s)):
        print(ans[i], end = " ")
 
# Driver code
if __name__ == "__main__":
   
    l = 9
    r = 21
    lexNumbers(l, r)
 
# This code is contributed by Chitranayal


C#
// C# program to implement the
// above approach
using System;
using System.Collections.Generic;
class GFG
{
 
  // Function to print all the
  // numbers form l to r in
  // lexicographical order
  static void lexNumbers(int l, int r)
  {
    List s = new List();
 
    for (int i = l; i <= r; i++)
    {
      s.Add(String.Join("", i));
    }
 
    s.Sort();
    List ans = new List();
 
    for (int i = 0; i < s.Count; i++)
      ans.Add(Int32.Parse(s[i]));
 
    for (int i = 0; i < s.Count; i++)
      Console.Write(ans[i] + " ");
  }
 
  // Driver Program
  static public void Main()
  {
    int l = 9;
    int r = 21;
    lexNumbers(l, r);
  }
}
 
// This code is contributed by Dharanendra L V


输出
1 10 11 12 13 14 15 2 3 4 5 6 7 8 9 

有范围时:-

给定两个整数L和R,任务是按词典顺序打印L到R(包括两端)范围内的所有数字。

例子:

Input: L = 9 , R = 21
Output:  
10 11 12 13 14 15 16 17 18 19 20 21 9
Input: L = 1 , R= 13
Output:  
1 10 11 12 13 2 3 4 5 6 7 8 9

方法:

为了解决问题,请按照以下步骤操作:

  • 从L迭代到R(包括端点),并以字符串形式存储所有数字。
  • 对包含字符串的向量进行排序。

下面是上述方法的实现:

C++

// C++ program to implement the
// above approach
#include 
using namespace std;
 
// Function to print all the
// numbers form l to r in
// lexicographical order
void lexNumbers(int l, int r)
{
    vector s;
 
    for(int i = l; i <= r; i++)
    {
        s.push_back(to_string(i));
    }
 
    sort(s.begin(),s.end());
    vector ans;
     
    for(int i = 0; i < s.size(); i++)
        ans.push_back(stoi(s[i]));
 
    for(int i = 0; i < s.size(); i++)
        cout << ans[i] << " ";
}
     
// Driver code
int main()
{
    int l = 9;
    int r = 21;
     
    lexNumbers(l, r);
}
 
// This code is contributed by ajaykr00kj

Java

// Java Program to implement the
// above approach
import java.util.*;
class GFG {
 
    // Function to print all the
    // numbers form l to r in
    // lexicographical order
    static void lexNumbers(int l, int r)
    {
        Vector s = new Vector();
 
        for (int i = l; i <= r; i++) {
            s.add(String.valueOf(i));
        }
 
        Collections.sort(s);
        Vector ans = new Vector();
        for (int i = 0; i < s.size(); i++)
            ans.add(Integer.valueOf(s.get(i)));
 
        for (int i = 0; i < s.size(); i++)
            System.out.print(ans.get(i) + " ");
    }
    // Driver Program
    public static void main(String[] args)
    {
        int l = 9;
        int r = 21;
        lexNumbers(l, r);
    }
}

Python3

# Python 3 program to implement
# the above approach
 
# Function to print all the
# numbers form l to r in
# lexicographical order
def lexNumbers(l, r):
 
    s = []
 
    for i in range(l, r + 1):
        s.append(str(i))
 
    s.sort()
    ans = []
 
    for i in range(len(s)):
        ans.append(int(s[i]))
 
    for i in range(len(s)):
        print(ans[i], end = " ")
 
# Driver code
if __name__ == "__main__":
   
    l = 9
    r = 21
    lexNumbers(l, r)
 
# This code is contributed by Chitranayal

C#

// C# program to implement the
// above approach
using System;
using System.Collections.Generic;
class GFG
{
 
  // Function to print all the
  // numbers form l to r in
  // lexicographical order
  static void lexNumbers(int l, int r)
  {
    List s = new List();
 
    for (int i = l; i <= r; i++)
    {
      s.Add(String.Join("", i));
    }
 
    s.Sort();
    List ans = new List();
 
    for (int i = 0; i < s.Count; i++)
      ans.Add(Int32.Parse(s[i]));
 
    for (int i = 0; i < s.Count; i++)
      Console.Write(ans[i] + " ");
  }
 
  // Driver Program
  static public void Main()
  {
    int l = 9;
    int r = 21;
    lexNumbers(l, r);
  }
}
 
// This code is contributed by Dharanendra L V
输出
10 11 12 13 14 15 16 17 18 19 20 21 9 

时间复杂度: O(N * logN)

辅助空间: O(1)