Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
```
Expand All @@ -134,39 +134,43 @@ 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;
}
};
```

#### 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
}
```

Expand All @@ -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<i32>, arr2: Vec<i32>) -> 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;
}
```

Expand All @@ -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;
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
```
Expand All @@ -132,39 +132,43 @@ 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;
}
};
```

#### 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
}
```

Expand All @@ -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<i32>, arr2: Vec<i32>) -> 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;
}
```

Expand All @@ -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;
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
};
Original file line number Diff line number Diff line change
@@ -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
}
if mx > 0 {
return len(strconv.Itoa(mx))
}
return 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Loading
Loading