Advertisement
Fastrail08

Lexicographic Numbers

Apr 27th, 2025
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. // Leetcode Discuss Topic
  2. // https://leetcode.com/discuss/post/6691579/issue-with-recursion-and-stringstream-co-5z40/
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. void printLex(int n, string ans, vector<int> &result){
  7.     stringstream ss(ans);
  8.     int num;
  9.     ss >> num;
  10.     if(num > n){
  11.         return;
  12.     }
  13.     cout << ans << '\n';
  14.     if(num != 0){
  15.     result.push_back(num);
  16.     }
  17.     for(int i = 0; i <= 9; i++){
  18.         if(ans == "" && i == 0){
  19.             continue;
  20.         }
  21.         else{
  22.             char numberToChar = i + '0';
  23.             printLex(n, ans + numberToChar, result);
  24.         }
  25.     }
  26. }
  27.  
  28. int main() {
  29.     // your code goes here
  30.     int n;
  31.     cin >> n;
  32.     vector<int> result;
  33.     printLex(n, "", result);
  34.     cout << "Vector: " << '\n';
  35.     for(int i : result){
  36.         cout << i << '\n';
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement