【发布时间】:2016-05-21 14:36:20
【问题描述】:
以下是我对leetcode中给出的同构字符串问题的解决方案:
public bool IsIsomorphic(string s, string t)
{
int[] s1 = new int[s.Length];
int[] t1 = new int[t.Length];
bool isI = true;
if (s1.Length > 0 && t1.Length > 0)
{
s1[0] = 0;
int i = 1;
int j = 1;
for (i = 1; i < s.Length; i++)
{
if (s[i] == s[i - 1])
{
s1[i] = 1;
}
else
{
s1[i] = 0;
}
}
for (j = 1; j < t.Length; j++)
{
if (t[j] == t[j - 1])
{
t1[j] = 1;
}
else
{
t1[j] = 0;
}
}
for (int k = 0; k < s1.Length; k++)
{
if(s1[k] != t1[k])
{
isI = false;
}
}
}
else
{
isI = true;
}
return isI;
}
它通过了 29/30 个测试用例,但没有通过以下长得离谱的测试用例。与谷歌驱动器中的输入共享代码:
https://docs.google.com/document/d/1UkG8Rc6VItiihwvqzJdM3uMHIX-BCsslJ_lVklkxvq8/edit?usp=sharing
任何帮助都会很棒。
【问题讨论】:
-
由于这是某种锻炼/测验网站,我想知道:您需要什么样的帮助:只需指出代码中的错误或正确的解决方案?
-
代码没有错误。我需要知道的是为什么只有 1 个测试用例会返回错误的输出,是因为什么?
-
“代码中没有错误”看起来与“仅 1 个测试用例返回错误输出”有点不一致,你不是说吗?
-
@AakashM 没有错误我的意思是没有编译器或运行时错误,很惊讶你没有这样考虑,为什么还要指出这样无用的东西?
标签: c# string algorithm string-matching string-algorithm