[問題] LeetCode 問題

看板C_Sharp (C#)作者 (呱)時間7年前 (2018/05/01 09:46), 7年前編輯推噓0(0014)
留言14則, 2人參與, 7年前最新討論串1/1
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://www.ptt.cc/bbs/C_Sharp/M.1525139205.A.F85.html

05/01 12:08, 7年前 , 1F
沒寫題目是要擲盃喔...
05/01 12:08, 1F
原本只是認為我單純詢問為何這段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 .

05/01 13:13, 7年前 , 2F
System.Collections.Generic.List`1[System.Char]這是你
05/01 13:13, 2F

05/01 13:14, 7年前 , 3F
把整個list丟進Console.WriteLine()印出來的吧?
05/01 13:14, 3F

05/01 13:15, 7年前 , 4F
ex: Console.WriteLine(list), list是一個List<Char>物件
05/01 13:15, 4F
先感謝您的回覆,這題目前已經用別的寫法解出來了 只不過還是好奇錯在哪 我跟你的想法一樣,他直接把我 list這個 object 給印出來 但我用 List.ToString()、 Array.ToString() 應該是直接把List內的value 給變成ToString() 才對呀@@ 為何會變成單純show list 而已?? ※ 編輯: ken52011219 (101.13.112.92), 05/01/2018 13:31:21

05/01 13:17, 7年前 , 5F
如果是的話,可能就是Console.Write()的參數放錯
05/01 13:17, 5F
※ 編輯: ken52011219 (101.13.112.92), 05/01/2018 13:37:21

05/01 13:40, 7年前 , 6F
List<>好像沒有實作ToString(),所以你的情境他做的是
05/01 13:40, 6F

05/01 13:40, 7年前 , 7F
Object.ToString()
05/01 13:40, 7F

05/01 13:41, 7年前 , 8F

05/01 13:42, 7年前 , 9F
object.ToString()是回傳GetType().ToString(); 做的是
05/01 13:42, 9F

05/01 13:43, 7年前 , 10F
Type.ToString()
05/01 13:43, 10F

05/01 13:45, 7年前 , 11F
Type.ToString()找一下MSDN可以發現就是印出型別
05/01 13:45, 11F

05/01 13:46, 7年前 , 12F
平時也沒記這麼細節的地方,剛剛臨時去查的,看看就好
05/01 13:46, 12F

05/01 13:49, 7年前 , 13F
123.ToString()能正確轉成字串是因為Int32.ToString()覆寫
05/01 13:49, 13F

05/01 13:50, 7年前 , 14F
了Object.ToString(),所以才能直覺的數字轉字串...吧
05/01 13:50, 14F
剛剛稍微在網路搜尋印證了t大的說法 List.ToString() 是 Object.ToString() 沒錯 ,非常感謝解惑<(_ _)> ※ 編輯: ken52011219 (101.13.112.92), 05/01/2018 13:59:06
文章代碼(AID): #1QvyS5-5 (C_Sharp)
文章代碼(AID): #1QvyS5-5 (C_Sharp)