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 @@ -174,14 +174,14 @@ func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) {
174
174
function longestCommonPrefix(arr1 : number [], arr2 : number []): number {
175
175
const s: Set <number > = new Set <number >();
176
176
for (let x of arr1 ) {
177
- for (; x ; x = (x / 10 ) | 0 ) {
178
- s .add (x % 10 );
177
+ for (; x ; x = Math . floor (x / 10 )) {
178
+ s .add (x );
179
179
}
180
180
}
181
181
let ans: number = 0 ;
182
182
for (let x of arr2 ) {
183
- for (; x ; x = (x / 10 ) | 0 ) {
184
- if (s .has (x % 10 )) {
183
+ for (; x ; x = Math . floor (x / 10 )) {
184
+ if (s .has (x )) {
185
185
ans = Math .max (ans , Math .floor (Math .log10 (x )) + 1 );
186
186
}
187
187
}
@@ -190,6 +190,33 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number {
190
190
}
191
191
```
192
192
193
+ #### JavaScript
194
+
195
+ ``` js
196
+ /**
197
+ * @param {number[]} arr1
198
+ * @param {number[]} arr2
199
+ * @return {number}
200
+ */
201
+ var longestCommonPrefix = function (arr1 , arr2 ) {
202
+ const s = new Set ();
203
+ for (let x of arr1) {
204
+ for (; x; x = Math .floor (x / 10 )) {
205
+ s .add (x);
206
+ }
207
+ }
208
+ let ans = 0 ;
209
+ for (let x of arr2) {
210
+ for (; x; x = Math .floor (x / 10 )) {
211
+ if (s .has (x)) {
212
+ ans = Math .max (ans, Math .floor (Math .log10 (x)) + 1 );
213
+ }
214
+ }
215
+ }
216
+ return ans;
217
+ };
218
+ ```
219
+
193
220
<!-- tabs: end -->
194
221
195
222
<!-- solution: end -->
You can’t perform that action at this time.
0 commit comments