Skip to content

Commit bd766a4

Browse files
authored
fix: update java solution to lc problem: No.2048 (#2075)
1 parent 6aef81e commit bd766a4

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

solution/2000-2099/2048.Next Greater Numerically Balanced Number/README.md

+13-17
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,22 @@ class Solution:
9191
```java
9292
class Solution {
9393
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];
9798
}
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;
111108
}
112109
}
113-
return true;
114110
}
115111
}
116112
```

0 commit comments

Comments
 (0)