Purnikmn:
ここでは元の質問へのリンクは次のとおりです。https://leetcode.com/problems/find-the-difference/
ここに私のコードです。私は、「インデックス範囲外」エラーを得続けるが、私は確かに理由はないです。
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
i = 0
ss = list(s)
tt = list(t)
while ss[i] == tt[i]:
i += 1
if ss[i] != tt[i]:
return tt[i]
マリオスStamatopoulos:
あなたが入力としてこれらの2を与えると言うことができます
s = "abcd"
t = "abcde"
あなたのループの第四円に私は= 3とするので、それは4になりss[i] != tt[i]
、それは次の円周上に行くだろうし、この式を評価します偽であるss[i] == tt[i]
とSSの長さは4であり、それは第五要素のdoesntのが存在し、それをアクセスしようとします最終的にはあなたのような何かを試みることができるはIndexErrorがスローされます。
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
i = 0
ss = list(s)+[None]
tt = list(t)
while ss[i] == tt[i]:
i += 1
if ss[i] != tt[i]:
return tt[i]