[问题] LeetCode 问题

楼主: ken52011219 (呱)   2018-05-01 09:46:40
public class Solution {
public string ToGoatLatin(string S)
{
String [] words = S.Split(' ');
List<char> vowel = new List<char>()
{
'a','e','i','o','u','A','E','I','O','U'
};
StringBuilder ans = new StringBuilder();
StringBuilder a = new StringBuilder();
foreach(string s in words) //s 正常
{
List<char> change_word = s.ToList();
if (vowel.IndexOf(change_word[0])==-1)
{
change_word.Insert(change_word.Count,change_word[0]);
change_word.RemoveAt(0);
}
a.Append("a");
ans.Append(" " + change_word.ToString() + "ma" + a);
}
return ans.Remove(ans.Length," " ).ToString() ;
}
}
目前COMPLIER 是过的,但怎么修改答案都是
" System.Collections.Generic.List`1[System.Char]maa
System.Collections.Generic.List`1[System.Char]maaa
System.Collections.Generic.List`1[System.Char]maaaa
System.Collections.Generic.List`1[System.Char]maaaaa"
但若以input "I speak Goat Latin" 为例,应该是要为
“ Imaa peaksmaaa oatGmaaaa atinLmaaaa”
第一次在板上发文,请各位大大解惑 感谢<(__ )>
作者: Litfal (Litfal)   2018-05-01 12:08:00
没写题目是要掷杯喔...
作者: t64141 (榕树)   2018-05-01 13:13:00
System.Collections.Generic.List`1[System.Char]这是你把整个list丢进Console.WriteLine()印出来的吧?ex: Console.WriteLine(list), list是一个List<Char>物件如果是的话,可能就是Console.Write()的参数放错List<>好像没有实作ToString(),所以你的情境他做的是Object.ToString()根据https://referencesource.microsoft.com/object.ToString()是回传GetType().ToString(); 做的是Type.ToString()Type.ToString()找一下MSDN可以发现就是印出型别平时也没记这么细节的地方,刚刚临时去查的,看看就好123.ToString()能正确转成字串是因为Int32.ToString()覆写了Object.ToString(),所以才能直觉的数字转字串...吧

Links booklink

Contact Us: admin [ a t ] ucptt.com