LeetCode 824. Goat Latin (山羊拉丁文)

题目标签:String

 首先把vowel letters 保存入 HashSet。

   然后把S 拆分成 各个 word,遍历每一个 word:

   当 word 第一个 字母不是 vowel 的时候,把第一个char 加到最后;

   然后添加“ma” 和 “a“ 到最后;

   添加新的"a";

   把新的 word 加入 result,还要记得加入空格。

 

Java Solution:

Runtime beats 62.66% 

完成日期:10/12/2018

关键词:String

关键点:利用HashSet保存vowel 

 1 class Solution 
 2 {
 3     public String toGoatLatin(String S) 
 4     {
 5         String result = "";
 6         Set<Character> vowelSet = new HashSet<>();
 7         String addOn = "a";
 8         
 9         for (char c: new char[]{'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'})
10             vowelSet.add(c);
11         
12         for(String word : S.split(" "))
13         {
14             if(result.length() > 0)
15                 result += " ";
16             
17             if(!vowelSet.contains(word.charAt(0)))
18             {
19                 word = word.substring(1) + word.charAt(0);
20             }
21             
22             word += "ma" + addOn;
23             addOn += "a";
24             
25             result += word;
26         }
27         
28         return result;
29     }
30 }

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

posted @ 2018-10-14 00:38  Jimmy_Cheng  阅读(431)  评论(0编辑  收藏  举报