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

Commit094e6d1

Browse files
[LEET-523] add 523
1 parentb504cf5 commit094e6d1

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

‎leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
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(n)|O(1)| Easy|
2324
|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|
2425
|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
2526
|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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packagecom.stevesun.solutions;
2+
3+
/**
4+
* Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings.
5+
* 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.
6+
* A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements.
7+
* Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.
8+
* The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.
9+
10+
Example 1:
11+
Input: "aba", "cdc"
12+
Output: 3
13+
Explanation: The longest uncommon subsequence is "aba" (or "cdc"),
14+
because "aba" is a subsequence of "aba",
15+
but not a subsequence of any other strings in the group of two strings.
16+
Note:
17+
18+
Both strings' lengths will not exceed 100.
19+
Only letters from a ~ z will appear in input strings.
20+
*/
21+
publicclassLongestUncommonSubsequenceI {
22+
//The gotcha point of this question is:
23+
//1. if a and b are identical, then there will be no common subsequence, return -1
24+
//2. else if a and b are of equal length, then any one of them will be a subsequence of the other string
25+
//3. else if a and b are of different length, then the longer one is a required subsequence because the longer string cannot be a subsequence of the shorter one
26+
publicintfindLUSlength(Stringa,Stringb) {
27+
if (a.equals(b))return -1;
28+
returnMath.max(a.length(),b.length());
29+
}
30+
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp