鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: zltzlt

Python:每日一题 376

[复制链接]
 楼主| 发表于 2020-4-15 21:40:53 | 显示全部楼层
TJBEST 发表于 2020-4-15 21:39
@zltzlt
多改了一个-1,这次应该对了。

好了,135 ms
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-15 22:40:32 From FishC Mobile | 显示全部楼层
本帖最后由 kinkon 于 2020-4-16 05:58 编辑

这时间差得有点多啊,看来优化空间很大
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-16 01:16:20 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-19 12:27:24 | 显示全部楼层
def f376(word1, word2):
    overlap = 0
    if len(word2) > len(word1):
        t = word1
        word1 = word2
        word2 = t

    for i in range(0, len(word1)):
        
        for j in range(0, len(word2)):

            if word1[i] == word2[j]:
                a, count = 0, 1

                while 1:
                    a += 1

                    if (i+a > len(word1)-1) or (j+a > len(word2)-1):
                        break

                    if (word1[i+a] == word2[j+a]):
                        count += 1
                    else:
                        break

                overlap = count if count > overlap else overlap
    return (len(word1) - overlap) + (len(word2) - overlap)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-19 13:58:37 | 显示全部楼层
def f376(word1, word2):
    overlap = 0
    
    for i in range(0, len(word1)):
        
        for j in range(0, len(word2)):
            
            if word1[i] == word2[j]:
                a, count = 0, 1
                
                while 1:
                    a += 1
                    
                    if (i+a > len(word1)-1) or (j+a > len(word2)-1):
                        break
                    
                    if (word1[i+a] == word2[j+a]):
                        count += 1
                    else:
                        break
                    
                overlap = count if count > overlap else overlap
                
    return (len(word1) - overlap) + (len(word2) - overlap)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-16 01:07:50 | 显示全部楼层
def daily376(world1="sssas", world2="sssts"):
    a = []
    b = []
    i = 0
    k = len(world1)
    t = len(world2)
    j = 0
    while i < k or j < t:
        if i < k:
            a.append(world1[i])
        if world2[j] in a:
            a.remove(world2[j])
            j += 1
        i += 1
        if i == k and j != t:
            if world2[j] not in a:
                b.append(world2[j])
                j += 1
    print(len(a) + len(b))


daily376()
时间,空间复杂度都为log(n),
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-16 01:11:36 | 显示全部楼层
def daily376(world1="sssas", world2="sssts"):
    a = []
    b = []
    i = 0
    k = len(world1)
    t = len(world2)
    j = 0
    while i < k or j < t:
        if i < k:
            a.append(world1[i])
        if world2[j] in a:
            a.remove(world2[j])
            j += 1
        i += 1
        if i >= k and j != t:
            if world2[j] not in a:
                b.append(world2[j])
                j += 1
    print(len(a) + len(b))


daily376()
遗漏一个条件world 1< world 2
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-15 22:01:24 | 显示全部楼层
def fun376(word1,word2):
    count = 0
    pointer = 0
    while pointer <= len(word2)-1:
        if word1 == '':
            count += 1
            pointer += 1
            continue
        elif word1[0] != word2[pointer]:
            word1 = word1[1:]
            count += 1
        elif word1[0] == word2[pointer]:
            word1 = word1[1:]
            pointer += 1
    return count
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-16 04:21:07 | 显示全部楼层
不会,来看答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 11:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表