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 4c0ea562d197f..bdd36e7c3579b 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 @@ -87,14 +87,14 @@ class Solution: while x: s.add(x) x //= 10 - ans = 0 + mx = 0 for x in arr2: while x: if x in s: - ans = max(ans, len(str(x))) + mx = max(mx, x) break x //= 10 - return ans + return len(str(mx)) if mx else 0 ``` #### Java @@ -108,16 +108,16 @@ class Solution { s.add(x); } } - int ans = 0; + int mx = 0; for (int x : arr2) { for (; x > 0; x /= 10) { if (s.contains(x)) { - ans = Math.max(ans, String.valueOf(x).length()); + mx = Math.max(mx, x); break; } } } - return ans; + return mx > 0 ? String.valueOf(mx).length() : 0; } } ``` @@ -134,16 +134,16 @@ public: s.insert(x); } } - int ans = 0; + int mx = 0; for (int x : arr2) { for (; x; x /= 10) { if (s.count(x)) { - ans = max(ans, (int) log10(x) + 1); + mx = max(mx, x); break; } } } - return ans; + return mx > 0 ? (int) log10(mx) + 1 : 0; } }; ``` @@ -151,22 +151,26 @@ public: #### Go ```go -func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { +func longestCommonPrefix(arr1 []int, arr2 []int) int { s := map[int]bool{} for _, x := range arr1 { for ; x > 0; x /= 10 { s[x] = true } } + mx := 0 for _, x := range arr2 { for ; x > 0; x /= 10 { if s[x] { - ans = max(ans, int(math.Log10(float64(x)))+1) + mx = max(mx, x) break } } } - return + if mx > 0 { + return len(strconv.Itoa(mx)) + } + return 0 } ``` @@ -180,15 +184,49 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { s.add(x); } } - let ans: number = 0; + let mx: number = 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); + mx = Math.max(mx, x); + break; + } + } + } + return mx > 0 ? Math.floor(Math.log10(mx)) + 1 : 0; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn longest_common_prefix(arr1: Vec, arr2: Vec) -> i32 { + let mut s = std::collections::HashSet::new(); + for x in arr1 { + let mut y = x; + while y > 0 { + s.insert(y); + y /= 10; + } + } + let mut mx = 0; + for x in arr2 { + let mut y = x; + while y > 0 { + if s.contains(&y) { + mx = mx.max(y); + break; + } + y /= 10; } } + if mx > 0 { + (mx as f64).log10().floor() as i32 + 1 + } else { + 0 + } } - return ans; } ``` @@ -207,15 +245,16 @@ var longestCommonPrefix = function (arr1, arr2) { s.add(x); } } - let ans = 0; + let mx = 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); + mx = Math.max(mx, x); + break; } } } - return ans; + return mx > 0 ? Math.floor(Math.log10(mx)) + 1 : 0; }; ``` 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 cb664d40f3541..de56472160881 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 @@ -85,14 +85,14 @@ class Solution: while x: s.add(x) x //= 10 - ans = 0 + mx = 0 for x in arr2: while x: if x in s: - ans = max(ans, len(str(x))) + mx = max(mx, x) break x //= 10 - return ans + return len(str(mx)) if mx else 0 ``` #### Java @@ -106,16 +106,16 @@ class Solution { s.add(x); } } - int ans = 0; + int mx = 0; for (int x : arr2) { for (; x > 0; x /= 10) { if (s.contains(x)) { - ans = Math.max(ans, String.valueOf(x).length()); + mx = Math.max(mx, x); break; } } } - return ans; + return mx > 0 ? String.valueOf(mx).length() : 0; } } ``` @@ -132,16 +132,16 @@ public: s.insert(x); } } - int ans = 0; + int mx = 0; for (int x : arr2) { for (; x; x /= 10) { if (s.count(x)) { - ans = max(ans, (int) log10(x) + 1); + mx = max(mx, x); break; } } } - return ans; + return mx > 0 ? (int) log10(mx) + 1 : 0; } }; ``` @@ -149,22 +149,26 @@ public: #### Go ```go -func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { +func longestCommonPrefix(arr1 []int, arr2 []int) int { s := map[int]bool{} for _, x := range arr1 { for ; x > 0; x /= 10 { s[x] = true } } + mx := 0 for _, x := range arr2 { for ; x > 0; x /= 10 { if s[x] { - ans = max(ans, int(math.Log10(float64(x)))+1) + mx = max(mx, x) break } } } - return + if mx > 0 { + return len(strconv.Itoa(mx)) + } + return 0 } ``` @@ -178,15 +182,49 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { s.add(x); } } - let ans: number = 0; + let mx: number = 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); + mx = Math.max(mx, x); + break; + } + } + } + return mx > 0 ? Math.floor(Math.log10(mx)) + 1 : 0; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn longest_common_prefix(arr1: Vec, arr2: Vec) -> i32 { + let mut s = std::collections::HashSet::new(); + for x in arr1 { + let mut y = x; + while y > 0 { + s.insert(y); + y /= 10; + } + } + let mut mx = 0; + for x in arr2 { + let mut y = x; + while y > 0 { + if s.contains(&y) { + mx = mx.max(y); + break; + } + y /= 10; } } + if mx > 0 { + (mx as f64).log10().floor() as i32 + 1 + } else { + 0 + } } - return ans; } ``` @@ -205,15 +243,16 @@ var longestCommonPrefix = function (arr1, arr2) { s.add(x); } } - let ans = 0; + let mx = 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); + mx = Math.max(mx, x); + break; } } } - return ans; + return mx > 0 ? Math.floor(Math.log10(mx)) + 1 : 0; }; ``` diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.cpp b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.cpp index 1cde1f70ec00c..780efb5133da0 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.cpp +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.cpp @@ -7,15 +7,15 @@ class Solution { s.insert(x); } } - int ans = 0; + int mx = 0; for (int x : arr2) { for (; x; x /= 10) { if (s.count(x)) { - ans = max(ans, (int) log10(x) + 1); + mx = max(mx, x); break; } } } - return ans; + return mx > 0 ? (int) log10(mx) + 1 : 0; } -}; \ No newline at end of file +}; diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.go b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.go index fedf8c00c152e..23e62ae8f43e3 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.go +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.go @@ -1,17 +1,21 @@ -func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { +func longestCommonPrefix(arr1 []int, arr2 []int) int { s := map[int]bool{} for _, x := range arr1 { for ; x > 0; x /= 10 { s[x] = true } } + mx := 0 for _, x := range arr2 { for ; x > 0; x /= 10 { if s[x] { - ans = max(ans, int(math.Log10(float64(x)))+1) + mx = max(mx, x) break } } } - return -} \ No newline at end of file + if mx > 0 { + return len(strconv.Itoa(mx)) + } + return 0 +} diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.java b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.java index 68ffa8bd4fc4d..fbeadc5dd2cef 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.java +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.java @@ -6,15 +6,15 @@ public int longestCommonPrefix(int[] arr1, int[] arr2) { s.add(x); } } - int ans = 0; + int mx = 0; for (int x : arr2) { for (; x > 0; x /= 10) { if (s.contains(x)) { - ans = Math.max(ans, String.valueOf(x).length()); + mx = Math.max(mx, x); break; } } } - return ans; + return mx > 0 ? String.valueOf(mx).length() : 0; } -} \ No newline at end of file +} 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 index 86762c5539377..5ee78cc294a41 100644 --- 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 @@ -10,13 +10,14 @@ var longestCommonPrefix = function (arr1, arr2) { s.add(x); } } - let ans = 0; + let mx = 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); + mx = Math.max(mx, x); + break; } } } - return ans; + return mx > 0 ? Math.floor(Math.log10(mx)) + 1 : 0; }; diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.py b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.py index 82c53e181189d..561521bc3fc35 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.py +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.py @@ -5,11 +5,11 @@ def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int: while x: s.add(x) x //= 10 - ans = 0 + mx = 0 for x in arr2: while x: if x in s: - ans = max(ans, len(str(x))) + mx = max(mx, x) break x //= 10 - return ans + return len(str(mx)) if mx else 0 diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.rs b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.rs new file mode 100644 index 0000000000000..5c899eed55df5 --- /dev/null +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.rs @@ -0,0 +1,28 @@ +impl Solution { + pub fn longest_common_prefix(arr1: Vec, arr2: Vec) -> i32 { + let mut s = std::collections::HashSet::new(); + for x in arr1 { + let mut y = x; + while y > 0 { + s.insert(y); + y /= 10; + } + } + let mut mx = 0; + for x in arr2 { + let mut y = x; + while y > 0 { + if s.contains(&y) { + mx = mx.max(y); + break; + } + y /= 10; + } + } + if mx > 0 { + (mx as f64).log10().floor() as i32 + 1 + } else { + 0 + } + } +} 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 f66c7309c52b2..ded4af21ef835 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 @@ -5,13 +5,14 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { s.add(x); } } - let ans: number = 0; + let mx: number = 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); + mx = Math.max(mx, x); + break; } } } - return ans; + return mx > 0 ? Math.floor(Math.log10(mx)) + 1 : 0; }