File tree Expand file tree Collapse file tree 1 file changed +31
-4
lines changed
solution/3000-3099/3043.Find the Length of the Longest Common Prefix Expand file tree Collapse file tree 1 file changed +31
-4
lines changed Original file line number Diff line number Diff line change @@ -176,14 +176,14 @@ func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) {
176
176
function longestCommonPrefix(arr1 : number [], arr2 : number []): number {
177
177
const s: Set <number > = new Set <number >();
178
178
for (let x of arr1 ) {
179
- for (; x ; x = (x / 10 ) | 0 ) {
180
- s .add (x % 10 );
179
+ for (; x ; x = Math . floor (x / 10 )) {
180
+ s .add (x );
181
181
}
182
182
}
183
183
let ans: number = 0 ;
184
184
for (let x of arr2 ) {
185
- for (; x ; x = (x / 10 ) | 0 ) {
186
- if (s .has (x % 10 )) {
185
+ for (; x ; x = Math . floor (x / 10 )) {
186
+ if (s .has (x )) {
187
187
ans = Math .max (ans , Math .floor (Math .log10 (x )) + 1 );
188
188
}
189
189
}
@@ -192,6 +192,33 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number {
192
192
}
193
193
```
194
194
195
+ #### JavaScript
196
+
197
+ ``` js
198
+ /**
199
+ * @param {number[]} arr1
200
+ * @param {number[]} arr2
201
+ * @return {number}
202
+ */
203
+ var longestCommonPrefix = function (arr1 , arr2 ) {
204
+ const s = new Set ();
205
+ for (let x of arr1) {
206
+ for (; x; x = Math .floor (x / 10 )) {
207
+ s .add (x);
208
+ }
209
+ }
210
+ let ans = 0 ;
211
+ for (let x of arr2) {
212
+ for (; x; x = Math .floor (x / 10 )) {
213
+ if (s .has (x)) {
214
+ ans = Math .max (ans, Math .floor (Math .log10 (x)) + 1 );
215
+ }
216
+ }
217
+ }
218
+ return ans;
219
+ };
220
+ ```
221
+
195
222
<!-- tabs: end -->
196
223
197
224
<!-- solution: end -->
You can’t perform that action at this time.
0 commit comments