File tree 1 file changed +13
-17
lines changed
solution/2000-2099/2048.Next Greater Numerically Balanced Number
1 file changed +13
-17
lines changed Original file line number Diff line number Diff line change @@ -91,26 +91,22 @@ class Solution:
91
91
``` java
92
92
class Solution {
93
93
public int nextBeautifulNumber (int n ) {
94
- for (int i = n + 1 ; i < 10000000 ; ++ i) {
95
- if (check(i)) {
96
- return i;
94
+ for (int x = n + 1 ;; ++ x) {
95
+ int [] cnt = new int [10 ];
96
+ for (int y = x; y > 0 ; y /= 10 ) {
97
+ ++ cnt[y % 10 ];
97
98
}
98
- }
99
- return - 1 ;
100
- }
101
-
102
- private boolean check (int num ) {
103
- int [] counter = new int [10 ];
104
- char [] chars = String . valueOf(num). toCharArray();
105
- for (char c : chars) {
106
- ++ counter[c - ' 0' ];
107
- }
108
- for (char c : chars) {
109
- if (counter[c - ' 0' ] != c - ' 0' ) {
110
- return false ;
99
+ boolean ok = true ;
100
+ for (int y = x; y > 0 ; y /= 10 ) {
101
+ if (y % 10 != cnt[y % 10 ]) {
102
+ ok = false ;
103
+ break ;
104
+ }
105
+ }
106
+ if (ok) {
107
+ return x;
111
108
}
112
109
}
113
- return true ;
114
110
}
115
111
}
116
112
```
You can’t perform that action at this time.
0 commit comments