作者ken52011219 (呱)
看板C_Sharp
标题[问题] LeetCode 问题
时间Tue May 1 09:46:40 2018
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」
第一次在板上发文,请各位大大解惑 感谢<(__ )>
--
有一个香锦囊,是从一个神话般的守军的血屍顶上剥下的。那一次我们部队遭受从未
有过的顽强抵抗,我们牺牲了三个舰队,一个装甲师和无以数计小组推进的敢死排,才摧
毁了那处隘口的碉堡。但是竟然发现,使我们遭受如此惨烈伤亡的守军,总数只有一人。
士兵们起哄地在他胸前发现这枚香袋,大家都相信这是一枚具有神奇力量的护身符。
我们把他的头颅砍断,取下香袋,剥开,
里面一张被血浸红的宣纸竟用汉字娟娟秀秀四个整齐的楷书写着-「盼君早归。」
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 101.13.112.92
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1525139205.A.F85.html
1F:→ Litfal: 没写题目是要掷盃喔... 05/01 12:08
原本只是认为我单纯询问为何这段CODE会产生这个问题,所以没打算贴题目出来
只不过依您要求
题目: #824 Goat Latin 难度:
easy
A sentence S is given, composed of words separated by spaces. Each word
consists of lowercase and uppercase letters only.
We would like to convert the sentence to "Goat Latin" (a made-up language
similar to Pig Latin.)
The rules of Goat Latin are as follows:
If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end
of the word.
For example, the word 'apple' becomes 'applema'.
If a word begins with a consonant (i.e. not a vowel), remove the first
letter and append it to the end, then add "ma".
For example, the word "goat" becomes "oatgma".
Add one letter 'a' to the end of each word per its word index in the
sentence, starting with 1.
For example, the first word gets "a" added to the end, the second word
gets "aa" added to the end and so on.
Return the final sentence representing the conversion from S to Goat Latin.
简单来说:
1. Input string , return string
2. If words' first char is a,e,i,o,u ,then append "ma" after the word
3. If words' first char isnt a,e,i,o,u , then the first char put
the words end.
4. increase 'a' per word and put it to the end word .
2F:→ t64141: System.Collections.Generic.List`1[System.Char]这是你 05/01 13:13
3F:→ t64141: 把整个list丢进Console.WriteLine()印出来的吧? 05/01 13:14
4F:→ t64141: ex: Console.WriteLine(list), list是一个List<Char>物件 05/01 13:15
先感谢您的回覆,这题目前已经用别的写法解出来了 只不过还是好奇错在哪
我跟你的想法一样,他直接把我 list这个 object 给印出来
但我用 List.ToString()、 Array.ToString() 应该是直接把List内的value
给变成ToString() 才对呀@@
为何会变成单纯show list 而已??
※ 编辑: ken52011219 (101.13.112.92), 05/01/2018 13:31:21
5F:→ t64141: 如果是的话,可能就是Console.Write()的参数放错 05/01 13:17
※ 编辑: ken52011219 (101.13.112.92), 05/01/2018 13:37:21
6F:→ t64141: List<>好像没有实作ToString(),所以你的情境他做的是 05/01 13:40
7F:→ t64141: Object.ToString() 05/01 13:40
9F:→ t64141: object.ToString()是回传GetType().ToString(); 做的是 05/01 13:42
10F:→ t64141: Type.ToString() 05/01 13:43
11F:→ t64141: Type.ToString()找一下MSDN可以发现就是印出型别 05/01 13:45
12F:→ t64141: 平时也没记这麽细节的地方,刚刚临时去查的,看看就好 05/01 13:46
13F:→ t64141: 123.ToString()能正确转成字串是因为Int32.ToString()覆写 05/01 13:49
14F:→ t64141: 了Object.ToString(),所以才能直觉的数字转字串...吧 05/01 13:50
刚刚稍微在网路搜寻印证了t大的说法
List.ToString() 是 Object.ToString() 没错 ,非常感谢解惑<(_ _)>
※ 编辑: ken52011219 (101.13.112.92), 05/01/2018 13:59:06