思路:直接使用int进行遍历,int有边界(使用数组模拟,或者使用全排列)
1 数组模拟法
public class Solution {
public void printToMaxOfNDedits(int n){
if(n < 0)
return;
if (n ==0 ){
System.out.println("0");
}
char [] num = new char[n];
//初始化
for (int i = 0; i < num.length; i++) {
num[i] = '0';
}
while(!increment(num)){
print(num);
}
}
private static boolean increment(char [] num){
boolean isEnd = false;
int len = num.length;
int nTakeOver = 0;//进位
for (int i = len - 1; i >= 0 ; i--) {
int nSum = num[i] - '0' + nTakeOver;//当前值
//只有是个位时,数值加1
if(i == len - 1)
nSum++;
//满10进1
if (nSum > 9){
if(i == 0){
//超过n位
isEnd = true;
}else {
nTakeOver = 1;
num[i] = '0';
}
}else {
num[i] = (char) ('0' + nSum);
break;
}
}
return isEnd;
}
private static void print(char [] num){
StringBuilder str = new StringBuilder();
boolean isBegin = false;
int len = num.length;
for (int i = 0; i < len; i++) {
if(num[i] != '0' && !isBegin){
isBegin = true;
}
if (isBegin){
str.append(String.valueOf(num[i]));
}
}
if(str.toString().matches("^9+$")){
System.out.println(str.toString()+" ");
} else {
System.out.print(str.toString()+" ");
}
}
}
2 全排列代替法
public class Solution {
public void printToMaxOfNDedits(int n){
if(n < 0)
return;
if (n == 0 ){
System.out.println("0");
return;
}
char [] num = new char[n];
//初始化
for (int i = 0; i < num.length; i++) {
num[i] = '0';
}
for (int i = 0; i < 10; i++) {
num[0] = (char) ('0' + i);
printToMaxOfNDeditsRecurstively(num, n, 0);
}
}
private static void printToMaxOfNDeditsRecurstively(char [] num,int length, int index){
if(index == length - 1){
print(num);
return;
}
for (int i = 0; i < 10; i++) {
num[index + 1] = (char)(i + '0');
printToMaxOfNDeditsRecurstively(num, length, index + 1);
}
}
private static void print(char [] num){
StringBuilder str = new StringBuilder();
boolean isBegin = false;
int len = num.length;
for (int i = 0; i < len; i++) {
if(num[i] != '0' && !isBegin){
isBegin = true;
}
if (isBegin){
str.append(String.valueOf(num[i]));
}
}
if(str.toString().matches("^9+$")){
System.out.println(str.toString()+" ");
} else {
System.out.print(str.toString()+" ");
}
}
}
扩展:char 最多能标识256个字符,十进制数:只用到10个,没有充分利用内存,有更高效的方式吗?有改char为byte
public class Solution {
public void printToMaxOfNDedits(int n){
if(n < 0)
return;
if (n ==0 ){
System.out.println("0");
}
byte [] num = new byte[n];
//初始化
for (int i = 0; i < num.length; i++) {
num[i] = 0;
}
while(!increment(num)){
print(num);
}
}
private static boolean increment(byte [] num){
boolean isEnd = false;
int len = num.length;
byte nTakeOver = 0;//进位
for (int i = len - 1; i >= 0 ; i--) {
byte nSum = (byte) (num[i] + nTakeOver);//byte相加,需要强转
//只有是个位时,数值加1
if(i == len - 1)
nSum++;
//满10进1
if (nSum > 9){
if(i == 0){
//超过n位
isEnd = true;
}else {
nTakeOver = 1;
num[i] = 0;
}
}else {
num[i] = nSum;
break;
}
}
return isEnd;
}
private static void print(byte [] num){
StringBuilder str = new StringBuilder();
boolean isBegin = false;
int len = num.length;
for (int i = 0; i < len; i++) {
if(num[i] != 0 && !isBegin){
isBegin = true;
}
if (isBegin){
str.append(String.valueOf(num[i]));
}
}
if(str.toString().matches("^9+$")){
System.out.println(str.toString()+" ");
} else {
System.out.print(str.toString()+" ");
}
}
}