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) {
174174function longestCommonPrefix(arr1 : number [], arr2 : number []): number {
175175 const s: Set <number > = new Set <number >();
176176 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 );
179179 }
180180 }
181181 let ans: number = 0 ;
182182 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 )) {
185185 ans = Math .max (ans , Math .floor (Math .log10 (x )) + 1 );
186186 }
187187 }
@@ -190,6 +190,33 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number {
190190}
191191```
192192
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+
193220<!-- tabs: end -->
194221
195222<!-- solution: end -->
You can’t perform that action at this time.
0 commit comments