我尝试编写程序,给定一个由 ascii[az] 范围内的小写字母组成的字符串,并确定包含该字符串中所有字母的最小子字符串的长度。
但由于超时我被终止了。
我怎样才能改善 sulotion?
我试过:
public static int shortestSubstring(string s){
int n = s.Length;
int max_distinct = max_distinct_char(s, n);
int minl = n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
String subs = null;
if (i < j)
subs = s.Substring(i, s.Length - j);
else
subs = s.Substring(j, s.Length - i);
int subs_lenght = subs.Length;
int sub_distinct_char = max_distinct_char(subs, subs_lenght);
if (subs_lenght < minl && max_distinct == sub_distinct_char)
{
minl = subs_lenght;
}
}
}
return minl;
}
private static int max_distinct_char(String s, int n)
{
int[] count = new int[NO_OF_CHARS];
for (int i = 0; i < n; i++)
count[s[i]]++;
int max_distinct = 0;
for (int i = 0; i < NO_OF_CHARS; i++)
{
if (count[i] != 0)
max_distinct++;
}
return max_distinct;
}
}
达令说
忽然笑
胡子哥哥
相关分类