Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdcaa3b2

Browse files
committed
feat: add LeetCode1239.
1 parent139271c commitdcaa3b2

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

‎src/com/yeahqing/medium/_1239/LeetCode1239.java‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
publicclassLeetCode1239 {
1616
publicstaticvoidmain(String[]args) {
17-
Solutionsolution =newSolution1();
17+
Solutionsolution =newSolution2();
1818
List<String>testCase =newArrayList<>(Arrays.asList("un","iq","ue"));
1919
System.out.println(solution.maxLength(testCase));
2020
}
@@ -59,6 +59,7 @@ public void dfs(List<Integer> masks, int pos, int mask) {
5959
classSolution1extendsSolution {
6060
@Override
6161
publicintmaxLength(List<String>arr) {
62+
// 使用哈希集合去重
6263
List<HashSet<Character>>all =newArrayList<>();
6364
all.add(newHashSet<>());
6465
intans =0;
@@ -80,3 +81,37 @@ public int maxLength(List<String> arr) {
8081
returnans;
8182
}
8283
}
84+
85+
classSolution2extendsSolution {
86+
@Override
87+
publicintmaxLength(List<String>arr) {
88+
intans =0;
89+
List<Integer>masks =newArrayList<>();
90+
masks.add(0);// 空串
91+
92+
for (Strings :arr) {
93+
// 1. 判断字符串中有没有重复的字符
94+
intmask =0;
95+
for (Characterc :s.toCharArray()) {
96+
intch =c -'a';
97+
if ((mask &1 <<ch) !=0) {
98+
mask =0;
99+
break;
100+
}
101+
// 没有重复字符,当前字符串就是可行解的一种
102+
mask |=1 <<ch;
103+
}
104+
// 有重复字符,直接跳过
105+
if (mask ==0)continue;
106+
// 2. 和前i项可行解做拼接,判断是否满足条件
107+
for (inti =0;i <masks.size();i++) {
108+
if ((mask &masks.get(i)) ==0) {
109+
// 和空串拼接,就是将当前串添加到可行解中
110+
masks.add(mask |masks.get(i));
111+
ans =Math.max(ans,Integer.bitCount(mask |masks.get(i)));
112+
}
113+
}
114+
}
115+
returnans;
116+
}
117+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp