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) {
176176function longestCommonPrefix(arr1 : number [], arr2 : number []): number {
177177 const s: Set <number > = new Set <number >();
178178 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 );
181181 }
182182 }
183183 let ans: number = 0 ;
184184 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 )) {
187187 ans = Math .max (ans , Math .floor (Math .log10 (x )) + 1 );
188188 }
189189 }
@@ -192,6 +192,33 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number {
192192}
193193```
194194
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+
195222<!-- tabs: end -->
196223
197224<!-- solution: end -->
You can’t perform that action at this time.
0 commit comments