zoukankan      html  css  js  c++  java
  • 824. Goat Latin山羊拉丁文

    [抄题]:

    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. 

    Example 1:

    Input: "I speak Goat Latin"
    Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
    

    Example 2:

    Input: "The quick brown fox jumped over the lazy dog"
    Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    以为最后再一起添加a,其实是每一个都加好 再拼到一起的

    [一句话思路]:

    元音建立新集合

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. 拆开的字母串、字符串可以直接放在等号边,更省事
    2. 字符串向后添加,直接用+=就行了

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    以为最后再一起添加a,其实是每一个都加好 再拼到一起的

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    for (j = 0, i++; j < i; ) 两个指针中j 每次清0,i不清零 来控制逐递增。头次见

    [关键模板化代码]:

    charat(0)第0位 && .substring(1)去除第0位之后 互相对应的

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    class Solution {
        public String toGoatLatin(String S) {
            //ini:store in set, res
            Set<Character> vowels = new HashSet<>();
            for (char c : "aeiouAEIOU".toCharArray()) {
                vowels.add(c);
            }
            String res = "";
            int i = 0, j = 0;
            
            //for loop
            for (String word : S.split("\s")) {
                res += ' ' + (vowels.contains(word.charAt(0)) ? word : word.substring(1) + word.charAt(0)) + "ma";
                
                //add a
                for (j = 0, i++; j < i; j++) {
                    res += 'a';
                } 
            }
          
            //return
            return res.substring(1);
        }
    }
    View Code
  • 相关阅读:
    FMDB(一)— 简单介绍
    产品设计之设计理念
    整理了一下浅墨大神的Visual C++/DirectX 9.0c的游戏开发手记
    使用scp免passwordserver间传递文件
    游戏架构其一:经常使用工具集合
    Failed to import package with error: Couldn't decompress package
    【从0開始Tornado建站】0.9版本号python站点代码开源--持续更新中
    【Android】 给我一个Path,还你一个酷炫动画
    codeforces Round #Pi (div.2) 567ABCD
    linux 查看磁盘使用情况
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8975027.html
Copyright © 2011-2022 走看看