地里新农-请到考试中心学习规则
- 积分
- 1
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-1-29
- 最后登录
- 1970-1-1
|
補充:第二輪的參考程式碼:
- // "static void main" must be defined in a public class.
- public class Solution {
- public static void main(String[] args) {
- new Solution();
- }
- public Solution() {
- int low = 1;
- int high = 650;
-
- Set<Integer> set = new HashSet<>();
- set.add(0);
- set.add(1);
- set.add(6);
- set.add(8);
- set.add(9);
- Set<Integer> ans = new TreeSet<>();
- // flip 6 --> 9
- // flip 9 --> 6
- for (int i = low; i < high; i++) {
- // divide the digit
- int ori = i;
- StringBuilder cur = new StringBuilder();
- while(ori > 0) {
- cur.append(ori % 10);
- ori /= 10;
- }
- cur.reverse();
-
- // check all digit is 0,1,6,8,9
- if (!allDigit_inSet(set, cur)) {
- continue;
- }
-
- // get flip value
- //System.out.println(cur);
- int[] mapping = {0, 1, -1, -1, -1, -1, 9, -1, 8, 6};
- StringBuilder cur_flip = new StringBuilder();
- for (int j = 0; j < cur.length(); j++) {
- //System.out.println( (int)cur.charAt(j) );
- cur_flip.append(mapping[cur.charAt(j) - 48]);
- }
- cur_flip.reverse();
-
- // compare cur and flip, if not equal then add to ans
- // if flip have leading zero don't all
- if (cur_flip.charAt(0) == '0') {
- continue;
- }
- // if flip greater than high
- if (Integer.parseInt( cur_flip.toString() ) > high) {
- continue;
- }
-
- for (int j = 0; j < cur.length(); j++) {
- if (cur.charAt(j) != cur_flip.charAt(j)) {
- ans.add(Integer.parseInt(cur.toString()));
- }
- }
- }
- System.out.println(ans);
- }
- boolean allDigit_inSet(Set<Integer> set, StringBuilder cur) {
- for (int j = 0; j < cur.length(); j++) {
- if (!set.contains(cur.charAt(j) - 48)) {
- return false;
- }
- }
- return true;
- }
- }
复制代码 |
|