392. 判断子序列 03.21 (一)

给定字符串 s 和 t ,判断 s 是否为 t 的子序列。

字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace""abcde"的一个子序列,而"aec"不是)。

进阶:

如果有大量输入的 S,称作 S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?

致谢:

特别感谢 @pbrother 添加此问题并且创建所有测试用例。

示例 1:

输入:s = "abc", t = "ahbgdc"
输出:true

示例 2:

输入:s = "axc", t = "ahbgdc"
输出:false

提示:

  • 0 <= s.length <= 100
  • 0 <= t.length <= 10^4
  • 两个字符串都只由小写字符组成。
  • public class Solution {
        public bool IsSubsequence(string s, string t) {
            int left = 0,right = 0;
            while(left < s.Length && right <t.Length){
                if(s[left] == t[right]) left++;
                    right++;
            }
            return left==s.Length;
        }
    }

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,根据您的要求,以下是Python代码实现: ``` import csv constellation_list = [ ('Aries', '03.21-04.19'), ('Taurus', '04.20-05.20'), ('Gemini', '05.21-06.21'), ('Cancer', '06.22-07.22'), ('Leo', '07.23-08.22'), ('Virgo', '08.23-09.22'), ('Libra', '09.23-10.23'), ('Scorpio', '10.24-11.22'), ('Sagittarius', '11.23-12.21'), ('Capricorn', '12.22-01.19'), ('Aquarius', '01.20-02.18'), ('Pisces', '02.19-03.20') ] def get_constellation(month, day): day_in_month = [ 0, # 0 位未使用 20, # 1 - 1 月 50, # 2 - 2 月 79, # 3 - 3 月 110, # 4 - 4 月 141, # 5 - 5 月 172, # 6 - 6 月 203, # 7 - 7 月 234, # 8 - 8 月 265, # 9 - 9 月 295, # 10 - 10 月 325, # 11 - 11 月 355, # 12 - 12 月 ] if month < 1 or month > 12 or day < 1 or day > 31: return "输入日期错误!" if day < day_in_month[month]: return constellation_list[month-1][0] else: return constellation_list[month][0] def get_constellation_by_date(date): try: year = int(date[0:4]) month = int(date[4:6]) day = int(date[6:8]) except: return "输入日期错误!" return get_constellation(month, day) with open('const.csv', 'r', encoding="UTF-8") as csvfile: reader = csv.reader(csvfile) for row in reader: name = row[0] month = int(row[1]) day = int(row[2]) correct_name = get_constellation(month, day) if correct_name != name: print('有错:{} 的生日是{}月{}日,应该是{}座,而不是{}座'.format(name, month, day, correct_name, name)) print(get_constellation_by_date('19930918')) print(get_constellation_by_date('20200231')) ``` 上面的代码中,我们在代码最开始定义了星座列表`constellation_list`,里面存放了每个星座的名字及其起止日期。 接着,我们定义了一个`get_constellation()`函数,用于根据月份和日期计算对应的星座名称。该函数的实现过程为:首先,创建了一个存放每月最后一天的列表`day_in_month`,其中0位未使用,1~12位为相应月份的最后一天;然后,如果输入的月份或日期不合法,就返回一个“输入日期错误!”的提示;否则,当输入的日期小于相应月份的最后一天时,返回相应的星座名称,否则返回下一个月份的星座名称。 接着,我们定义了一个`get_constellation_by_date()`函数,用于读取`const.csv`文件并根据用户输入的出生日期获取对应的星座名称。该函数首先尝试将输入日期的年、月、日解析成整数,如果出错则返回一个“输入日期错误!”的提示;否则,调用`get_constellation()`函数计算对应的星座名称并返回。 最后,我们读取了`const.csv`文件并分别调用了`get_constellation_by_date()`函数获取了两个日期的星座名称,分别为白羊座和“输入日期错误!”(因为2月份只有28/29天,而这里输入的是30号,所以日期不合法)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小路灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值