華圖教育-面試題1-1到10000之前,有多少升數字?

【請問1到10000之前,有多少升數字?】
升數字就是從左向右讀,數值是依次上升的即可,比如123,1256,1389,但是1123,165就不是。請提供明確的實現思路的僞代碼,或者提供寫好的代碼(證明你是一個合格的coder),請在拿道題後的1.5小時內回覆,過期相當於放棄後面面試機會。

此題屬於原創題,百度查找應該沒有答案。加油!

1:思路既然比如1234這個數字,右邊的數字比左邊的大,可以使用棧,將左邊的數字入棧。右邊的數字 與彈棧的數字比較。

import java.util.Stack;


/**
 * 
 * @date 2018年6月12日 下午10:07:14
 */
public class Test2 {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
int cnt = 0;
Boolean flag = false;
for (int i = 1; i <= 1000000000; i++) {
String numToStr = String.valueOf(i);
int numLength = numToStr.length();
flag = false;
for (int j = 0; j < numLength; j++) {
int currentNum = (int) numToStr.charAt(j);
if (stack.isEmpty()) {
stack.push(currentNum);
} else {
int topNum = stack.pop();
// 棧頂的數字 >= 現在的數字
if (topNum >= currentNum) {
flag = true;
break;
} else {
stack.push(currentNum);
}
}
}
            if (!flag) {
cnt++;
System.out.print(i); System.out.print(" ");System.out.println(cnt);
}
stack.clear();

}
System.out.println(cnt);
}


}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章