• 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
  • 相关阅读:
    python的多线程问题
    python 调用函数时使用星号 *, **
    python正则中的贪婪与非贪婪
    python中文处理
    Python 模块之 ConfigParser: 用 Python 解析配置文件
    substr使用注意
    [转]互联网后台服务的协议设计
    Java设计模式从精通到入门四 工厂方法模式
    logback中logger详解
    logback实践笔记
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8975027.html
Copyright © 2020-2023  润新知