zoukankan      html  css  js  c++  java
  • 824. Goat Latin

    Questioin

    824. Goat Latin

    Solution

    题目大意:根据要求翻译句子

    思路:转换成单词数组,遍历数组,根据要求转换单词

    Java实现:

    用Java8的流实现,效率太低

    public String toGoatLatin(String S) {
        final String[] arr = S.split(" ");
        final int[] idx = {0};
        return Arrays.stream(S.split(" "))
            .map(s -> convert(s, ++idx[0]))
            .reduce("", (s1, s2) -> s1 + " " + s2).trim();
    }
    
    String convert(String ori, int count) {
        String pre = "";
        // begin with vowel aeiou
        char first = ori.charAt(0);
        if (first == 'A' || first == 'a'
            || first == 'E' || first == 'e'
            || first == 'I' || first == 'i'
            || first == 'O' || first == 'o'
            || first == 'U' || first == 'u'
           ) {
            pre = ori;
        } else {
            // begin with consonant not aeiou
            pre = ori.substring(1) + first;
        }
    
        // add a
        char[] a = new char[count];
        for (int i = 0; i < count; i++) {
            a[i] = 'a';
        }
        return pre + "ma" + String.valueOf(a);
    }
    

    public String toGoatLatin(String S) {
        StringBuilder sb = new StringBuilder();
        int count = 1;
        for(String tmp : S.split(" ")) {
            sb.append(convert(tmp, count++)).append(" ");
        }
        return sb.toString().trim();
    }
    

  • 相关阅读:
    js 数组相减
    js 对象数组去重
    vue 首次不触发watch的解决方法
    "神药"推荐--紫花地丁
    openstack 平台添加 nvidia vGPU
    string易错点整理总结
    CentOS安装图形界面
    CentOs安装ssh和scp
    数组把0移到末尾
    Windows下Celery安装与下使用
  • 原文地址:https://www.cnblogs.com/okokabcd/p/9470373.html
Copyright © 2011-2022 走看看