@@ -81,6 +81,153 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3646.Ne
81
81
#### Java
82
82
83
83
``` java
84
+ class Solution {
85
+ private static List<Long > pool = new ArrayList<> ();
86
+ private static boolean inited = false ;
87
+
88
+ public long specialPalindrome (long n ) {
89
+ if (! inited) {
90
+ synchronized (Solution . class) {
91
+ if (! inited) {
92
+ initPool();
93
+ inited = true ;
94
+ }
95
+ }
96
+ }
97
+
98
+ int idx = Collections . binarySearch(pool, n);
99
+ if (idx < 0 ) {
100
+ idx = - idx - 1 ;
101
+ } else {
102
+ idx++ ;
103
+ }
104
+ return pool. get(idx);
105
+ }
106
+
107
+ private void initPool () {
108
+ Consumer<String > addIfOk = s - > {
109
+ if (s. isEmpty() || s. length() >= 17 ) {
110
+ return ;
111
+ }
112
+ try {
113
+ long value = Long . parseLong(s);
114
+ pool. add(value);
115
+ } catch (NumberFormatException e) {
116
+ }
117
+ };
118
+
119
+ BiConsumer<int[], Consumer<String > > gen = (cnt, adder) - > {
120
+ int totalLength = 0 ;
121
+ for (int d = 1 ; d <= 9 ; d++ ) {
122
+ totalLength += cnt[d];
123
+ }
124
+ if (totalLength == 0 || totalLength > 17 ) {
125
+ return ;
126
+ }
127
+
128
+ int [] halfCnt = new int [10 ];
129
+ for (int d = 1 ; d <= 9 ; d++ ) {
130
+ halfCnt[d] = cnt[d] / 2 ;
131
+ }
132
+
133
+ final int halfLength;
134
+ {
135
+ int temp = 0 ;
136
+ for (int d = 1 ; d <= 9 ; d++ ) {
137
+ temp += halfCnt[d];
138
+ }
139
+ halfLength = temp;
140
+ }
141
+
142
+ final char mid;
143
+ {
144
+ char tempMid = 0 ;
145
+ for (int d = 1 ; d <= 9 ; d++ ) {
146
+ if ((cnt[d] & 1 ) == 1 ) {
147
+ tempMid = (char ) (' 0' + d);
148
+ break ;
149
+ }
150
+ }
151
+ mid = tempMid;
152
+ }
153
+
154
+ StringBuilder half = new StringBuilder ();
155
+ int [] hc = halfCnt. clone();
156
+
157
+ Runnable dfs = new Runnable () {
158
+ @Override
159
+ public void run () {
160
+ if (half. length() == halfLength) {
161
+ String reversed = new StringBuilder (half). reverse(). toString();
162
+ String palindrome;
163
+ if (mid != 0 ) {
164
+ palindrome = half. toString() + mid + reversed;
165
+ } else {
166
+ palindrome = half. toString() + reversed;
167
+ }
168
+ adder. accept(palindrome);
169
+ return ;
170
+ }
171
+
172
+ for (int d = 1 ; d <= 9 ; d++ ) {
173
+ if (hc[d] == 0 ) {
174
+ continue ;
175
+ }
176
+ hc[d]-- ;
177
+ half. append((char ) (' 0' + d));
178
+ this . run();
179
+ half. deleteCharAt(half. length() - 1 );
180
+ hc[d]++ ;
181
+ }
182
+ }
183
+ };
184
+
185
+ dfs. run();
186
+ };
187
+
188
+ int [] evenDigits = {2 , 4 , 6 , 8 };
189
+ int [] oddDigits = {1 , 3 , 5 , 7 , 9 };
190
+
191
+ for (int mask = 1 ; mask < (1 << 4 ); mask++ ) {
192
+ int [] cnt = new int [10 ];
193
+ int totalLength = 0 ;
194
+ for (int i = 0 ; i < 4 ; i++ ) {
195
+ if ((mask & (1 << i)) != 0 ) {
196
+ int d = evenDigits[i];
197
+ cnt[d] = d;
198
+ totalLength += d;
199
+ }
200
+ }
201
+ if (totalLength <= 17 ) {
202
+ gen. accept(cnt, addIfOk);
203
+ }
204
+ }
205
+
206
+ for (int odd : oddDigits) {
207
+ for (int mask = 0 ; mask < (1 << 4 ); mask++ ) {
208
+ int [] cnt = new int [10 ];
209
+ int totalLength = odd;
210
+ cnt[odd] = odd;
211
+ for (int i = 0 ; i < 4 ; i++ ) {
212
+ if ((mask & (1 << i)) != 0 ) {
213
+ int d = evenDigits[i];
214
+ cnt[d] = d;
215
+ totalLength += d;
216
+ }
217
+ }
218
+ if (totalLength <= 17 ) {
219
+ gen. accept(cnt, addIfOk);
220
+ }
221
+ }
222
+ }
223
+
224
+ Collections . sort(pool);
225
+ List<Long > uniquePool = new ArrayList<> (new LinkedHashSet<> (pool));
226
+ pool. clear();
227
+ pool. addAll(uniquePool);
228
+ }
229
+ }
230
+
84
231
85
232
```
86
233
0 commit comments