[問題] 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