zoukankan      html  css  js  c++  java
  • *LeetCode--Goat Latin

    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"
    

    Notes:

    • S contains only uppercase, lowercase and spaces. Exactly one space between each word.

    自己的解法:

    就是对整个字符串的每一个字符利用空格进行判断,得出一个一个单独的单词,然后在对含有元音的单词和辅音的单词进行各自独立的单词,虽然做出来了,但是效果不好,很笨重,看到别人使用很多字符串现有的方法,contains(), charAt(), 这些我也使用了,但是像 trim()  split()  substring()  就没想到,就是自己不会向那方面去想,但是看到别人的方法,就  哇!  好巧妙~~的感觉。

    看来还是刷题刷少了,继续努力~_~

    class Solution {
        public String toGoatLatin(String S) {
            if(S == null || S.length() == 0) return "a";
            StringBuilder str = new StringBuilder();
            int i = 0;
            String s = "ma";
            while(i < S.length()){
                s += "a";
                boolean flag = false;
                if(S.charAt(i) == 'a' || S.charAt(i) == 'e' 
                   || S.charAt(i) == 'i' || S.charAt(i) == 'o' 
                   ||S.charAt(i) == 'u' || S.charAt(i) == 'A' || S.charAt(i) == 'E' 
                   || S.charAt(i) == 'I' || S.charAt(i) == 'O' 
                   ||S.charAt(i) == 'U')
                    flag = true;
                if(flag){
                    while(i < S.length() && ' ' != S.charAt(i)){
                        str.append(S.charAt(i));
                        i++;
                    }
                    str.append(s);
                } else{
                    char ch = S.charAt(i++);
                    while(i < S.length() && ' ' != S.charAt(i)){
                        str.append(S.charAt(i));
                        i++;
                    }
                    str.append(ch).append(s);
                }
                if(i != S.length()) str.append(' ');
                i++;
            }
            return str.toString();
        }
    }
    

      

    别人的解法:先放上,等过一段时间再来重新做一遍,争取用到很好的方法(起码不是自己那么笨的方法)

    class Solution {
        public String toGoatLatin(String S) {
            String vowel = "aeiouAEIOU";
            String result="";
            String addA = "a";
            for (String word:S.split(" ")){
                if (vowel.contains(word.charAt(0)+"")){
                    result+=word+"ma"+addA+" ";
                }
                else {
                    result+=word.substring(1,word.length())+word.charAt(0)+"ma"+addA+" ";
                }
                addA+="a";
            }
            return result.trim();
        }
    }
    

      

  • 相关阅读:
    动态规划算法——最长公共子序列问题(java实现)
    算法java实现--动态规划--电路布线问题
    动态规划经典问题Java实现
    使用WebRTC搭建前端视频聊天室
    Safari支不支持HTML5录音? 现在浏览器中最好的解决方案是WebRTC下的 navigator.getUserMedia API。
    java 实现websocket的两种方式
    媒体文件audio 转 base64 编码 (利用 FileReader & Audio 对象)
    jquery 图片文件转base64 显示
    blob转base64位 base64位转blob
    websocket消息推送实现
  • 原文地址:https://www.cnblogs.com/SkyeAngel/p/9062499.html
Copyright © 2011-2022 走看看