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.反转字符串中的元音字母 #74

Open
zzcyes opened this issue Jul 29, 2020 · 1 comment
Open

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

zzcyes opened this issue Jul 29, 2020 · 1 comment
Assignees
Labels
Level: ⭐ Easy ⭐ Two Pointers Two Pointers

Comments

@zzcyes
Copy link
Owner

zzcyes commented Jul 29, 2020

Title Describe
题目 345.反转字符串中的元音字母
难度

题目

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

示例 1:

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

示例 2:

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

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

@zzcyes zzcyes added Level: ⭐ Easy ⭐ Two Pointers Two Pointers labels Jul 29, 2020
@zzcyes
Copy link
Owner Author

zzcyes commented Jul 29, 2020

题解

当左指针和右指针匹配的字符串都为元音时匹配即可。

方法一:双指针

/**
 * @param {string} s
 * @return {string}
 */
var reverseVowels = function(s) {
  let vowels = ['a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'];
  s = s.split('');
  let p1 = 0;
  let p2 = s.length - 1;
  while (p1 < p2) {
    if (vowels.includes(s[p1]) && vowels.includes(s[p2])) {
      [s[p1], s[p2]] = [s[p2], s[p1]];
      p1++;
      p2--;
    } else if (!vowels.includes(s[p1])) {
      p1++;
    } else if (!vowels.includes(s[p2])) {
      p2--;
    }
  }
  return s.join('');
};

zzcyes pushed a commit that referenced this issue Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Level: ⭐ Easy ⭐ Two Pointers Two Pointers
Projects
None yet
Development

No branches or pull requests

2 participants