Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

345. 反转字符串中的元音字母 #60

Open
yankewei opened this issue Oct 9, 2020 · 1 comment
Open

345. 反转字符串中的元音字母 #60

yankewei opened this issue Oct 9, 2020 · 1 comment
Labels
双指针 题目包含双指针解法 字符串 题目类型为字符串 简单 题目难度为简单

Comments

@yankewei
Copy link
Owner

yankewei commented Oct 9, 2020

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

示例 1:

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

示例 2:

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

提示:

元音字母不包含字母 "y" 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-vowels-of-a-string

@yankewei yankewei added 双指针 题目包含双指针解法 简单 题目难度为简单 字符串 题目类型为字符串 labels Oct 9, 2020
@yankewei
Copy link
Owner Author

yankewei commented Oct 9, 2020

双指针

挺容易想到的

func reverseVowels(s string) string {
    m := map[string]bool{
        "a": true,
        "e": true,
        "i": true,
        "o": true,
        "u": true,
        "A": true,
        "E": true,
        "I": true,
        "O": true,
        "U": true,
    }
    sSlice := strings.Split(s, "")
    l, r := 0, len(s)-1
    for l < r {
        _, e := m[sSlice[l]];
        if !e {
            l++
            continue
        }
        _, e = m[sSlice[r]];
        if !e {
            r--
            continue
        }
        sSlice[l], sSlice[r] = sSlice[r], sSlice[l]
        l++
        r--
    }
    return strings.Join(sSlice, "")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
双指针 题目包含双指针解法 字符串 题目类型为字符串 简单 题目难度为简单
Projects
None yet
Development

No branches or pull requests

1 participant