## Monday, August 8, 2016

### Google – Generate All Numbers in Range

Google – Generate All Numbers in Range

a = 99, b = 100这样的就不行。
[Solution]

1. 如果sb[i – 1] == a[i – 1]，那么当前level从a[i]开始，否则从0开始。
2. 如果sb[i – 1] == b[i – 1]，那么当前level到b[i]结束，否则到9结束。

[Note]

`  ``public` `List<String> allNumbers(String a, String b) {`
`    ``List<String> result = ``new` `ArrayList<>();`
`    ``while` `(a.length() < b.length()) {`
`      ``a = ``"0"` `+ a;`
`    ``}`

`    ``backtracking(a, b, ``0``, result, ``new` `StringBuilder());`
`    ``return` `result;`
`  ``}`

`  ``private` `void` `backtracking(String a, String b, ``int` `pos, List<String> result, StringBuilder sb) {`
`    ``if` `(pos == a.length()) {`
`      ``result.add(sb.toString());`
`      ``return``;`
`    ``}`

`    ``int` `i = ``0``;`
`    ``if` `(pos == ``0` `|| sb.charAt(pos - ``1``) == a.charAt(pos - ``1``)) {`
`      ``i = a.charAt(pos) - ``'0'``;`
`    ``}`

`    ``int` `j = ``9``;`
`    ``if` `(pos == ``0` `|| sb.charAt(pos - ``1``) == b.charAt(pos - ``1``)) {`
`      ``j = b.charAt(pos) - ``'0'``;`
`    ``}`

`    ``while` `(i <= j) {`
`      ``sb.append(i);`
`      ``if` `(i < j) {`
`        ``backtracking(a, b, pos + ``1``, result, sb);`
`      ``}`
`      ``else` `{`
`        ``backtracking(a, b, pos + ``1``, result, sb);`
`      ``}`
`      ``sb.deleteCharAt(sb.length() - ``1``);`
`      ``i++;`
`    ``}`
`  ``}`