Thursday, November 8, 2018
386. Lexicographical Numbers
Version #1 DFS
40.18 %
class Solution {
public List<Integer> lexicalOrder(int n) {
List<Integer> result = new ArrayList<>();
if (n < 0) {
return result;
}
dfs(0, n, result);
return result;
}
private void dfs(int curr, int n, List<Integer> result) {
for (int i = 0; i < 10; i++) {
if (curr == 0 && i == 0) {
continue;
}
int x = curr * 10 + i;
if (x > n) {
return;
}
result.add(x);
dfs(x, n, result);
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment