Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Lexicographical Number problem solution

YASH PAL, 31 July 2024

In this Leetcode Lexicographical Number problem solution you have given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order. You must write an algorithm that runs in O(n) time and uses O(1) extra space.

Leetcode Lexicographical Number problem solution

Problem solution in Python.

class Solution:
    def lexicalOrder(self, n: int) -> List[int]:
        l = list()
        for i in range(1,n+1):
            l.append(str(i))
        l.sort()
        i = len(l)-1
        while i>0:
            l[i] = int(l[i])
            i-=1
        l[i] = int(l[i])
        return l

Problem solution in Java.

public List<Integer> lexicalOrder(int n) {
        int num = 1;
        List<Integer> results = new ArrayList<>();
        while (true) {
            if (num <= n) {
                results.add(num);
                num *= 10;
                continue;
            }
            
            num /= 10;
            while (num % 10 == 9 && num != 0)
                num /= 10;
            if (num == 0)
                return results;
            num += 1;
        }
    }

Problem solution in C++.

class Solution {
    int next(int last, int n)
    {
        if (last * 10 <= n)
            return last * 10;
        if (last == n)
            last /= 10;
        ++last;
        while(last % 10 == 0)
            last /= 10;
        return last;
    }
public:
    vector<int> lexicalOrder(int n) {
        if (n < 1) return vector<int>();
        vector<int> result(n);
        result[0] = 1;
        for (int i = 1; i < n; ++ i)
            result[i] = next(result[i - 1], n);
        return result;
    }
};

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes