LeetCode Weekly Contest 26
[2017-04-02]
- Longest Uncommon Subsequence I
- Longest Uncommon Subsequence II
- Friend Circles
- Split Array with Equal Sum
Longest Uncommon Subsequence I
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. 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.
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.
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.
Example 1:
Note:
- Both strings’ lengths will not exceed 100.
- Only letters from a ~ z will appear in input strings.
code
两个字符串相同就无解;
否则输出两个字符串的长度中较大者;
Longest Uncommon Subsequence II
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.
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.
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.
Example 1:
Note:
- All the given strings’ lengths will not exceed 10.
- The length of the given list will be in the range of [2, 50].
code
因为最大长度为10;
所以把每个长度有哪些字符串记录下来;
然后从字符串长度由大到小枚举len;
假设这个答案序列为len长度的某个字符串;
然后看看len长度的字符串有没有和它一样的字符串(即出现两次及以上)
有的话不行,找另外一个长度为len的字符串;
否则
再看看这个字符串是不是长度比len长的字符串的子串;
如果不是的话;
就表示找到了;
直接输出len;
否则继续找长度为len的另外的字符串;
判断一个字符串是不是另外一个字符串的子串其实很简单的;
O(l1+l2)就能判断出来;
Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.
Given a N*N matrix M representing the friend relationship between students in the class. If M[i][j] = 1, then the ith and jth students are direct friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.
Example 1:
Example 2:
Note:
- N is in range [1,200].
- M[i][i] = 1 for all students.
- If M[i][j] = 1, then M[j][i] = 1.
code
|
|
Split Array with Equal Sum
Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions:
- 0 < i, i + 1 < j, j + 1 < k < n - 1
- Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be equal.
where we define that subarray (L, R) represents a slice of the original array starting from the element indexed L to the element indexed R.
Example:
Note:
- 1 <= n <= 2000.
- Elements in the given array will be in range [-1,000,000, 1,000,000].
code
先枚举要删除的3个元素中的中间那个元素j;
然后把整个序列分成左边和右边两个部分;
然后再左边的序列中枚举i;
然后把0..j-1分成0..i-1和i+1..j-1两个部分;
然后看这两个部分各自的和是不是相同;
相同的话把和用map记录一下;
然后再枚举
右半部分即第3个元素k;
分成
j+1..k-1和k+1..n-1两个部分;
看看各自的和是不是相同;
相同的话,看看刚才左半部分记录的map里面有没有这个和;
有的话输出true
复杂度接近O(n^2)吧->用了map哦。