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

Commit18f8a18

Browse files
[LEET-522] add 522
1 parentdd224af commit18f8a18

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

‎leetcode-algorithms/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
|529|[Minesweeper](https://leetcode.com/problems/minesweeper/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/Minesweeper.java) | O(m*n) |O(k) | Medium | BFS
2121
|526|[Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/BeautifulArrangement.java) | O(n) |O(h) | Medium | Backtracking
2222
|523|[Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ContinuousSubarraySum.java)| O(n^2)|O(n)| Medium|
23-
|521|[Longest Uncommon Subsequence I](https://leetcode.com/problems/longest-uncommon-subsequence-i/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LongestUncommonSubsequenceI.java)| O(max(x,y))|O(1)| Easy|
23+
|522|[Longest Uncommon Subsequence II](https://leetcode.com/problems/longest-uncommon-subsequence-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LongestUncommonSubsequenceII.java)| O(x*n^2) (x is average length of strings)|O(1)| Medium|
24+
|521|[Longest Uncommon Subsequence I](https://leetcode.com/problems/longest-uncommon-subsequence-i/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/LongestUncommonSubsequenceI.java)| O(max(x,y)) (x and y are length of strings)|O(1)| Easy|
2425
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/DetectCapital.java)| O(n)|O(1)| Easy|
2526
|515|[Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindLargestValueinEachTreeRow.java) | O(n) |O(k) | Medium| BFS
2627
|513|[Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/FindBottomLeftValue.java) | O(n) |O(k) | Medium| BFS
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
packagecom.stevesun.solutions;
2+
3+
importjava.util.Arrays;
4+
importjava.util.Comparator;
5+
6+
/**
7+
* Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.
8+
9+
A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.
10+
11+
The input will be a list of strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.
12+
13+
Example 1:
14+
Input: "aba", "cdc", "eae"
15+
Output: 3
16+
Note:
17+
18+
All the given strings' lengths will not exceed 10.
19+
The length of the given list will be in the range of [2, 50].
20+
*/
21+
publicclassLongestUncommonSubsequenceII {
22+
23+
//Idea: if there's such a LUS there in the list, it must be one of the strings in the given list,
24+
//so we'll just go through the list and check if one string is NOT subsequence of any others, if so, return it, otherwise, return -1
25+
publicintfindLUSlength(String[]strs) {
26+
Arrays.sort(strs,newComparator<String>() {
27+
@Override
28+
publicintcompare(Stringo1,Stringo2) {
29+
returno2.length() -o1.length();
30+
}
31+
});
32+
33+
for (inti =0;i <strs.length;i++) {
34+
booleanfound =true;
35+
for (intj =0;j <strs.length;j++) {
36+
if (i ==j)continue;
37+
elseif (isSubsequence(strs[i],strs[j])) {
38+
found =false;
39+
break;
40+
}
41+
}
42+
if (found)returnstrs[i].length();
43+
}
44+
return -1;
45+
}
46+
47+
privatebooleanisSubsequence(Stringa,Stringb) {
48+
inti =0;
49+
for (intj =0;i <a.length() &&j <b.length();j++) {
50+
if (a.charAt(i) ==b.charAt(j))i++;
51+
}
52+
returni ==a.length();
53+
}
54+
55+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packagecom.stevesun;
2+
3+
importcom.stevesun.solutions.LongestUncommonSubsequenceII;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticjunit.framework.Assert.assertEquals;
8+
9+
/**
10+
* Created by stevesun on 12/31/16.
11+
*/
12+
publicclassLongestUncommonSubsequenceIITest {
13+
14+
privatestaticLongestUncommonSubsequenceIItest;
15+
privatestaticintexpected;
16+
privatestaticintactual;
17+
privatestaticString[]strs;
18+
19+
@BeforeClass
20+
publicstaticvoidsetup(){
21+
test =newLongestUncommonSubsequenceII();
22+
}
23+
24+
@Test
25+
publicvoidtest1(){
26+
strs =newString[]{"aaa","aaa","aa"};
27+
expected = -1;
28+
actual =test.findLUSlength(strs);
29+
assertEquals(expected,actual);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp