Google OA(HH:MM) Posted on 2017-09-16 | In Java , Algorithm | input给一个format “00:00” - “23:59” 的时间,里面每位数字可以重复使用,问组成的下一个时间是多少。 例23:59 —> 22:22 直接暴力 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455class Solution { public String nextTime(String time) { StringBuffer res = new StringBuffer(); TreeSet<Integer> set = new TreeSet<>(); for(int i=0; i<5; i++){ if (i == 2) continue; set.add(time.charAt(i)-48); } int point = 0; // 最后一位 if (set.higher(time.charAt(4)-48) == null) point = 1; if (point == 1) res.append(set.first()); else res.append(set.higher(time.charAt(4)-48)); // 倒数第二 if (point == 0) res.append(time.charAt(3)); else { if (set.higher(time.charAt(3) - 48) == null || set.higher(time.charAt(3) - 48) > 5) res.append(set.first()); else { res.append(set.higher(time.charAt(3) - 48)); point = 0; } } res.append(':'); if (point == 0) res.append(time.charAt(1)); else { if (time.charAt(0) == 2) { if (set.higher(time.charAt(1) - 48) == null || set.higher(time.charAt(1) - 48) > 3) res.append(set.first()); else { res.append(set.higher(time.charAt(1) - 48)); point = 0; } } else{ if (set.higher(time.charAt(1) - 48) == null) res.append(set.first()); else { res.append(set.higher(time.charAt(1) - 48)); point = 0; } } } if (point == 0) res.append(time.charAt(0)); else{ if (set.higher(time.charAt(0)-48) == null || set.higher(time.charAt(0)-48) > 2) res.append(set.first()); else res.append(set.higher(time.charAt(0)-48)); } res.reverse(); return res.toString(); }} 自己随便测试了一下,应该可行。