[日常刷题]leetcode D42

版权声明:希望各位多提意见多多互相交流哦~ https://blog.csdn.net/wait_for_taht_day5/article/details/83247573

521. 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:

Input: "aba", "cdc"
Output: 3
Explanation: The longest uncommon subsequence is "aba" (or "cdc"), 
because "aba" is a subsequence of "aba", 
but not a subsequence of any other strings in the group of two strings.

Note:

  1. Both strings’ lengths will not exceed 100.
  2. Only letters from a ~ z will appear in input strings.

Solution in C++:

关键点:

  • 分类讨论

思路:

  1. 哇,开始还以为很难,最开始写的代码只是处理一下我认为的特殊情况,然后在想其他情况的case的时候,我想了几个,比如acb与ab,我以为会输出2,但是结果不是,我就想到了因为前面不相等直接输出其中的长度最大值了,然后再就想不出来能跨过这个的例子了,打算submit一下,看有没有错误提示,结果AC了,满脸震惊,然后去看题解居然也是这样做的,看了分析才认识到真正的原理。真是分析大法好啊。
    解析
int findLUSlength(string a, string b) {
        if (a != b)
            return max(a.size(), b.size());
        else
            return -1;
    }

551. Student Attendance Record I

You are given a string representing an attendance record for a student. The record only contains the following three characters:

  1. ’A’ : Absent.
  2. ’L’ : Late.
  3. ’P’ : Present.
    A student could be rewarded if his attendance record doesn’t contain more than one ‘A’ (absent) or more than two continuous ‘L’ (late).

You need to return whether the student could be rewarded according to his attendance record.

Example 1:

Input: "PPALLP"
Output: True

Example 2:

Input: "PPALLL"
Output: False

Solution in C++:

关键点:

  • 连续的L

思路:

  • 开始没看到连续,然后老是错,还以为自己逻辑看错了,后来发现是题意理解错了(不过也是通过例子才知道的),接下来就是连续L计数的问题,开始把不是连续L的值设置为0出现了逻辑问题,后来就把不是连续的L设置成1就解决了。大框架就是遍历就好。然后可能这里还有就是a和l的值判断的时机应该放在改变值之后,以免遇到字符串结尾才出现False的情况。
  • 看到discuss里面判断连续L的逻辑是碰到P之后将l的值归零,其他情况正常+1,感觉这个逻辑也蛮不错的,点个赞。
bool checkRecord(string s) {
        int a = 0;
        int l = 0;
        
        for(int i = 0; i < s.size(); ++i){
            if (s[i] == 'A')
                ++a;
            else if (s[i] == 'L'){
                if (i != 0 && s[i-1] == 'L')
                    ++l;
                else
                    l = 1;
            }
            if (a > 1)
                return false;
            if (l > 2)
                return false;
        }
        
        return true;
    }

小结

今天实验室出去聚会了,回来比较晚了,明天还有课还要去实验室打卡,今天就刷不到那么长时间了,但是不过却有不少收获,思维上的锻炼得到了,很享受。

知识点

  • 分类讨论

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/83247573
今日推荐