Skip to content

Commit be7598c

Browse files
authored
feat: add solutions for lc No.3043 (#5195)
1 parent 6d42378 commit be7598c

9 files changed

Lines changed: 169 additions & 57 deletions

File tree

solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ class Solution:
8787
while x:
8888
s.add(x)
8989
x //= 10
90-
ans = 0
90+
mx = 0
9191
for x in arr2:
9292
while x:
9393
if x in s:
94-
ans = max(ans, len(str(x)))
94+
mx = max(mx, x)
9595
break
9696
x //= 10
97-
return ans
97+
return len(str(mx)) if mx else 0
9898
```
9999

100100
#### Java
@@ -108,16 +108,16 @@ class Solution {
108108
s.add(x);
109109
}
110110
}
111-
int ans = 0;
111+
int mx = 0;
112112
for (int x : arr2) {
113113
for (; x > 0; x /= 10) {
114114
if (s.contains(x)) {
115-
ans = Math.max(ans, String.valueOf(x).length());
115+
mx = Math.max(mx, x);
116116
break;
117117
}
118118
}
119119
}
120-
return ans;
120+
return mx > 0 ? String.valueOf(mx).length() : 0;
121121
}
122122
}
123123
```
@@ -134,39 +134,43 @@ public:
134134
s.insert(x);
135135
}
136136
}
137-
int ans = 0;
137+
int mx = 0;
138138
for (int x : arr2) {
139139
for (; x; x /= 10) {
140140
if (s.count(x)) {
141-
ans = max(ans, (int) log10(x) + 1);
141+
mx = max(mx, x);
142142
break;
143143
}
144144
}
145145
}
146-
return ans;
146+
return mx > 0 ? (int) log10(mx) + 1 : 0;
147147
}
148148
};
149149
```
150150
151151
#### Go
152152
153153
```go
154-
func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) {
154+
func longestCommonPrefix(arr1 []int, arr2 []int) int {
155155
s := map[int]bool{}
156156
for _, x := range arr1 {
157157
for ; x > 0; x /= 10 {
158158
s[x] = true
159159
}
160160
}
161+
mx := 0
161162
for _, x := range arr2 {
162163
for ; x > 0; x /= 10 {
163164
if s[x] {
164-
ans = max(ans, int(math.Log10(float64(x)))+1)
165+
mx = max(mx, x)
165166
break
166167
}
167168
}
168169
}
169-
return
170+
if mx > 0 {
171+
return len(strconv.Itoa(mx))
172+
}
173+
return 0
170174
}
171175
```
172176

@@ -180,15 +184,49 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number {
180184
s.add(x);
181185
}
182186
}
183-
let ans: number = 0;
187+
let mx: number = 0;
184188
for (let x of arr2) {
185189
for (; x; x = Math.floor(x / 10)) {
186190
if (s.has(x)) {
187-
ans = Math.max(ans, Math.floor(Math.log10(x)) + 1);
191+
mx = Math.max(mx, x);
192+
break;
193+
}
194+
}
195+
}
196+
return mx > 0 ? Math.floor(Math.log10(mx)) + 1 : 0;
197+
}
198+
```
199+
200+
#### Rust
201+
202+
```rust
203+
impl Solution {
204+
pub fn longest_common_prefix(arr1: Vec<i32>, arr2: Vec<i32>) -> i32 {
205+
let mut s = std::collections::HashSet::new();
206+
for x in arr1 {
207+
let mut y = x;
208+
while y > 0 {
209+
s.insert(y);
210+
y /= 10;
211+
}
212+
}
213+
let mut mx = 0;
214+
for x in arr2 {
215+
let mut y = x;
216+
while y > 0 {
217+
if s.contains(&y) {
218+
mx = mx.max(y);
219+
break;
220+
}
221+
y /= 10;
188222
}
189223
}
224+
if mx > 0 {
225+
(mx as f64).log10().floor() as i32 + 1
226+
} else {
227+
0
228+
}
190229
}
191-
return ans;
192230
}
193231
```
194232

@@ -207,15 +245,16 @@ var longestCommonPrefix = function (arr1, arr2) {
207245
s.add(x);
208246
}
209247
}
210-
let ans = 0;
248+
let mx = 0;
211249
for (let x of arr2) {
212250
for (; x; x = Math.floor(x / 10)) {
213251
if (s.has(x)) {
214-
ans = Math.max(ans, Math.floor(Math.log10(x)) + 1);
252+
mx = Math.max(mx, x);
253+
break;
215254
}
216255
}
217256
}
218-
return ans;
257+
return mx > 0 ? Math.floor(Math.log10(mx)) + 1 : 0;
219258
};
220259
```
221260

solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ class Solution:
8585
while x:
8686
s.add(x)
8787
x //= 10
88-
ans = 0
88+
mx = 0
8989
for x in arr2:
9090
while x:
9191
if x in s:
92-
ans = max(ans, len(str(x)))
92+
mx = max(mx, x)
9393
break
9494
x //= 10
95-
return ans
95+
return len(str(mx)) if mx else 0
9696
```
9797

9898
#### Java
@@ -106,16 +106,16 @@ class Solution {
106106
s.add(x);
107107
}
108108
}
109-
int ans = 0;
109+
int mx = 0;
110110
for (int x : arr2) {
111111
for (; x > 0; x /= 10) {
112112
if (s.contains(x)) {
113-
ans = Math.max(ans, String.valueOf(x).length());
113+
mx = Math.max(mx, x);
114114
break;
115115
}
116116
}
117117
}
118-
return ans;
118+
return mx > 0 ? String.valueOf(mx).length() : 0;
119119
}
120120
}
121121
```
@@ -132,39 +132,43 @@ public:
132132
s.insert(x);
133133
}
134134
}
135-
int ans = 0;
135+
int mx = 0;
136136
for (int x : arr2) {
137137
for (; x; x /= 10) {
138138
if (s.count(x)) {
139-
ans = max(ans, (int) log10(x) + 1);
139+
mx = max(mx, x);
140140
break;
141141
}
142142
}
143143
}
144-
return ans;
144+
return mx > 0 ? (int) log10(mx) + 1 : 0;
145145
}
146146
};
147147
```
148148
149149
#### Go
150150
151151
```go
152-
func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) {
152+
func longestCommonPrefix(arr1 []int, arr2 []int) int {
153153
s := map[int]bool{}
154154
for _, x := range arr1 {
155155
for ; x > 0; x /= 10 {
156156
s[x] = true
157157
}
158158
}
159+
mx := 0
159160
for _, x := range arr2 {
160161
for ; x > 0; x /= 10 {
161162
if s[x] {
162-
ans = max(ans, int(math.Log10(float64(x)))+1)
163+
mx = max(mx, x)
163164
break
164165
}
165166
}
166167
}
167-
return
168+
if mx > 0 {
169+
return len(strconv.Itoa(mx))
170+
}
171+
return 0
168172
}
169173
```
170174

@@ -178,15 +182,49 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number {
178182
s.add(x);
179183
}
180184
}
181-
let ans: number = 0;
185+
let mx: number = 0;
182186
for (let x of arr2) {
183187
for (; x; x = Math.floor(x / 10)) {
184188
if (s.has(x)) {
185-
ans = Math.max(ans, Math.floor(Math.log10(x)) + 1);
189+
mx = Math.max(mx, x);
190+
break;
191+
}
192+
}
193+
}
194+
return mx > 0 ? Math.floor(Math.log10(mx)) + 1 : 0;
195+
}
196+
```
197+
198+
#### Rust
199+
200+
```rust
201+
impl Solution {
202+
pub fn longest_common_prefix(arr1: Vec<i32>, arr2: Vec<i32>) -> i32 {
203+
let mut s = std::collections::HashSet::new();
204+
for x in arr1 {
205+
let mut y = x;
206+
while y > 0 {
207+
s.insert(y);
208+
y /= 10;
209+
}
210+
}
211+
let mut mx = 0;
212+
for x in arr2 {
213+
let mut y = x;
214+
while y > 0 {
215+
if s.contains(&y) {
216+
mx = mx.max(y);
217+
break;
218+
}
219+
y /= 10;
186220
}
187221
}
222+
if mx > 0 {
223+
(mx as f64).log10().floor() as i32 + 1
224+
} else {
225+
0
226+
}
188227
}
189-
return ans;
190228
}
191229
```
192230

@@ -205,15 +243,16 @@ var longestCommonPrefix = function (arr1, arr2) {
205243
s.add(x);
206244
}
207245
}
208-
let ans = 0;
246+
let mx = 0;
209247
for (let x of arr2) {
210248
for (; x; x = Math.floor(x / 10)) {
211249
if (s.has(x)) {
212-
ans = Math.max(ans, Math.floor(Math.log10(x)) + 1);
250+
mx = Math.max(mx, x);
251+
break;
213252
}
214253
}
215254
}
216-
return ans;
255+
return mx > 0 ? Math.floor(Math.log10(mx)) + 1 : 0;
217256
};
218257
```
219258

solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ class Solution {
77
s.insert(x);
88
}
99
}
10-
int ans = 0;
10+
int mx = 0;
1111
for (int x : arr2) {
1212
for (; x; x /= 10) {
1313
if (s.count(x)) {
14-
ans = max(ans, (int) log10(x) + 1);
14+
mx = max(mx, x);
1515
break;
1616
}
1717
}
1818
}
19-
return ans;
19+
return mx > 0 ? (int) log10(mx) + 1 : 0;
2020
}
21-
};
21+
};
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) {
1+
func longestCommonPrefix(arr1 []int, arr2 []int) int {
22
s := map[int]bool{}
33
for _, x := range arr1 {
44
for ; x > 0; x /= 10 {
55
s[x] = true
66
}
77
}
8+
mx := 0
89
for _, x := range arr2 {
910
for ; x > 0; x /= 10 {
1011
if s[x] {
11-
ans = max(ans, int(math.Log10(float64(x)))+1)
12+
mx = max(mx, x)
1213
break
1314
}
1415
}
1516
}
16-
return
17-
}
17+
if mx > 0 {
18+
return len(strconv.Itoa(mx))
19+
}
20+
return 0
21+
}

solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ public int longestCommonPrefix(int[] arr1, int[] arr2) {
66
s.add(x);
77
}
88
}
9-
int ans = 0;
9+
int mx = 0;
1010
for (int x : arr2) {
1111
for (; x > 0; x /= 10) {
1212
if (s.contains(x)) {
13-
ans = Math.max(ans, String.valueOf(x).length());
13+
mx = Math.max(mx, x);
1414
break;
1515
}
1616
}
1717
}
18-
return ans;
18+
return mx > 0 ? String.valueOf(mx).length() : 0;
1919
}
20-
}
20+
}

0 commit comments

Comments
 (0)