88 - 反转字符串中的元音字母

题目

编写一个函数,以字符串作为输入,反转该字符串中的元音字母。

示例 1:

输入: "hello" 输出: "holle"

示例 2:

输入: "leetcode" 输出: "leotcede"

说明: 元音字母不包含字母"y"。

解答

双指针?遇到了换个位置?

如果保留字符串的话,要用到splice再join,感觉很烦。不如打散成数组,对换也容易,最后拼起来就行了

var reverseVowels = function (s) {
  s = s.split('')
  vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
  for (let i = 0, j = s.length - 1; i < j;) {
    if (vowels.indexOf(s[i]) !== -1 && vowels.indexOf(s[j]) !== -1) {
      [s[i], s[j]] = [s[j], s[i]]
      i++
      j--
    }
    if (vowels.indexOf(s[i]) === -1) {
      i++
    }
    if (vowels.indexOf(s[j]) === -1) {
      j--
    }
  }
  return s.join("")
};

Runtime: 84 ms, faster than 45.35% of JavaScript online submissions for Reverse Vowels of a String.

Memory Usage: 38.4 MB, less than 92.86% of JavaScript online submissions for Reverse Vowels of a String.

虽然通过了,但感觉这个做法有点挫。。做了一些无用功。。

func reverseVowels(s string) string {
    data := map[string]bool{
        "a": true,
        "e": true,
        "i": true,
        "o": true,
        "u": true,
        "A": true,
        "E": true,
        "I": true,
        "O": true,
        "U": true,
    }
    sr := []rune(s)
    for i, j := 0, len(s)-1; i < j; {
        _, oki := data[string(sr[i])]
        _, okj := data[string(sr[j])]
        if oki && okj {
            sr[i], sr[j] = sr[j], sr[i]
            i++
            j--
            continue
        }
        if !oki {
            i++
        }
        if !okj {
            j--
        }
    }
    return string(sr)
}

Runtime: 8 ms, faster than 31.58% of Go online submissions for Reverse Vowels of a String.

Memory Usage: 4.6 MB, less than 66.67% of Go online submissions for Reverse Vowels of a String.

把map的true换成int,内存就会多1mb

func reverseVowels(s string) string {
    data := map[string]int{
        "a": 1,
        "e": 1,
        "i": 1,
        "o": 1,
        "u": 1,
        "A": 1,
        "E": 1,
        "I": 1,
        "O": 1,
        "U": 1,
    }
    ...
}

Runtime: 8 ms, faster than 31.58% of Go online submissions for Reverse Vowels of a String.

Memory Usage: 4.7 MB, less than 66.67% of Go online submissions for Reverse Vowels of a String.

参考了这个题解,用切片和strings包试试看:

func reverseVowels(s string) string {
    vowels := "aeiouAEIOU"
    st := []rune(s)
    for start, end := 0, len(s)-1; start < end; {
        if strings.ContainsRune(vowels, st[start]) && strings.ContainsRune(vowels, st[end]) {
            st[start], st[end] = st[end], st[start]
            start++
            end--
            continue
        }
        if !strings.ContainsRune(vowels, st[start]) {
            start++
        }
        if !strings.ContainsRune(vowels, st[end]) {
            end--
        }
    }
    return string(st)
}

Runtime: 4 ms, faster than 81.95% of Go online submissions for Reverse Vowels of a String.

Memory Usage: 4.5 MB, less than 66.67% of Go online submissions for Reverse Vowels of a String.

巨快。。

比哈希表还快,不知道这个strings的containsRune是怎么实现的。。

Last updated

Was this helpful?