We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f4825db commit 8c24319Copy full SHA for 8c24319
solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts
@@ -1,17 +1,24 @@
1
function longestCommonPrefix(arr1: number[], arr2: number[]): number {
2
- const s: Set<number> = new Set<number>();
+ let set = new Set<number>();
3
+
4
for (let x of arr1) {
- for (; x; x = (x / 10) | 0) {
5
- s.add(x % 10);
+ while (x > 0) {
6
+ set.add(x);
7
+ x = Math.floor(x / 10);
8
}
9
- let ans: number = 0;
10
11
+ let ans = 0;
12
13
for (let x of arr2) {
- if (s.has(x % 10)) {
14
15
+ if (set.has(x)) {
16
ans = Math.max(ans, Math.floor(Math.log10(x)) + 1);
17
+ break;
18
19
20
21
22
23
return ans;
24
0 commit comments