diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md index b4d7746119489..a501e904cf3be 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md @@ -176,14 +176,14 @@ func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { function longestCommonPrefix(arr1: number[], arr2: number[]): number { const s: Set = new Set(); for (let x of arr1) { - for (; x; x = (x / 10) | 0) { - s.add(x % 10); + for (; x; x = Math.floor(x / 10)) { + s.add(x); } } let ans: number = 0; for (let x of arr2) { - for (; x; x = (x / 10) | 0) { - if (s.has(x % 10)) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); } } @@ -192,6 +192,33 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { } ``` +#### JavaScript + +```js +/** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number} + */ +var longestCommonPrefix = function (arr1, arr2) { + const s = new Set(); + for (let x of arr1) { + for (; x; x = Math.floor(x / 10)) { + s.add(x); + } + } + let ans = 0; + for (let x of arr2) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + } + } + } + return ans; +}; +``` + diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md index 1ff77d43c30de..6dab65a426ad3 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md @@ -174,14 +174,14 @@ func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { function longestCommonPrefix(arr1: number[], arr2: number[]): number { const s: Set = new Set(); for (let x of arr1) { - for (; x; x = (x / 10) | 0) { - s.add(x % 10); + for (; x; x = Math.floor(x / 10)) { + s.add(x); } } let ans: number = 0; for (let x of arr2) { - for (; x; x = (x / 10) | 0) { - if (s.has(x % 10)) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); } } @@ -190,6 +190,33 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { } ``` +#### JavaScript + +```js +/** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number} + */ +var longestCommonPrefix = function (arr1, arr2) { + const s = new Set(); + for (let x of arr1) { + for (; x; x = Math.floor(x / 10)) { + s.add(x); + } + } + let ans = 0; + for (let x of arr2) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + } + } + } + return ans; +}; +``` + diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js new file mode 100644 index 0000000000000..86762c5539377 --- /dev/null +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number} + */ +var longestCommonPrefix = function (arr1, arr2) { + const s = new Set(); + for (let x of arr1) { + for (; x; x = Math.floor(x / 10)) { + s.add(x); + } + } + let ans = 0; + for (let x of arr2) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + } + } + } + return ans; +}; diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts index 88c02857fb7fe..f66c7309c52b2 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts @@ -1,14 +1,14 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { const s: Set = new Set(); for (let x of arr1) { - for (; x; x = (x / 10) | 0) { - s.add(x % 10); + for (; x; x = Math.floor(x / 10)) { + s.add(x); } } let ans: number = 0; for (let x of arr2) { - for (; x; x = (x / 10) | 0) { - if (s.has(x % 10)) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); } }