[日常刷题]leetcode D42

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;
    }

小结

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

知识点

  • 分类讨论
Python 是一种流行的高级编程语言,因其简洁易读的语法和广泛的应用领域而受到开发者喜爱。LeetCode 是一个在线编程平台,专门用于算法和技术面试的准备,提供了大量的编程题目,包括数据结构、算法、系统设计等,常用于提升程序员的编程能力和解决实际问题的能力。 在 Python 中刷 LeetCode 题目通常涉及以下步骤: 1. **注册账户**:首先在 LeetCode 官网 (https://leetcode.com/) 注册一个账号,这样你可以跟踪你的进度和提交的代码。 2. **选择语言**:登录后,在个人主页设置中选择 Python 作为主要编程语言。 3. **学习和理解题目**:阅读题目描述,确保你理解问题的要求和预期输出。题目通常附有输入示例和预期输出,可以帮助你初始化思考。 4. **编写代码**:使用 Python 编写解决方案,LeetCode 提供了一个在线编辑器,支持实时预览和运行结果。 5. **测试和调试**:使用给出的测试用例来测试你的代码,确保它能够正确地处理各种边界条件和特殊情况。 6. **提交答案**:当代码完成后,点击 "Submit" 提交你的解法。LeetCode 会自动运行所有测试用例并显示结果。 7. **学习他人的代码**:如果遇到困难,可以查看社区中的其他优秀解法,学习他人的思路和技术。 8. **反复练习**:刷题不是一次性的事情,通过反复练习和优化,逐渐提高解题速度和代码质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值