首先没有考虑到双指针,主要是 if 条件的判断,没有想到 typed 重复中和前一个对比,还有,即使自己重写一遍也缺少了一些判断条件 :
1> 有 i-1,要考虑 i>0
2> np.array([1,2,3])=[1 2 3]
参考答案是
name = "laiden"
typed = "aidden"
l1, l2 = len(name), len(typed)
i, j, a = 0, 0, 0
while j < l2:
if i < l1 and name[i] == typed[j]:
i += 1
j += 1
elif j > 0 and typed[j] == typed[j - 1]:
j += 1
else:
a += 1
break
if a == 0:
print("True")
else:
print("False")
我的代码用了两个 for,想数相同字母的个数,然后做差,有一个负数就是错误,但是有点长
import numpy as np
name = "laiden"
typed = "laiden"
n1, n2 = [], []
a, b, d = 1, 1, 0
name1 = list(name)
typed1 = list(typed)
for i in range(len(name1)-1):
if name1[i] == name1[i + 1]:
a += 1
else:
n1.append(a)
a = 1
for j in range(len(typed1)-1):
if typed1[j] == typed1[j + 1]:
b += 1
else:
n2.append(b)
b = 1
c = np.array(n2) - np.array(n1)
for k in c:
if k < 0:
d += 1
if d > 0:
print("False")
else:
print("True")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)