zoukankan      html  css  js  c++  java
  • leetCode题解之反转字符串中的元音字母

    1、问题描述

    Reverse Vowels of a String

    Write a function that takes a string as input and reverse only the vowels of a string.  

    Example 1: Given s = "hello", return "holle".  

    Example 2: Given s = "leetcode", return "leotcede".

     Note: The vowels does not include the letter "y".

    题目要求是将一个输入string 中的元音字母位置反转,即第一个元音字母和最后一个元音字母交换位置。

    ,以此类推。

    2、题目分析

        题目和反转字符串很相似,只不过需要判断一个字符是否为元音字母。思路是两个指针一个从前往后遍历,遇到元音字母停下;另外一个从后往前遍历,遇到元音字母停下。然后交换位置。

    3、代码

     1 string reverseVowels(string s) {
     2         
     3         string vow = "aeiouAEIOU";
     4         int i ,j;
     5         
     6         for( i = 0,j = s.size() - 1; i < j ; )
     7         {
     8             if( vow.find( s[i]) != string::npos && vow.find( s[j] ) != string::npos )
     9                 swap(s[i],s[j]),i++,j--;
    10             if(vow.find( s[i]) == string::npos )
    11                 i++;
    12             if( vow.find(s[j]) == string::npos )
    13                 j--;
    14         }
    15         
    16         return s;
    17         
    18     }
    pp
  • 相关阅读:
    html笔记3
    html笔记2
    html学习第一天
    用Vue中的指令写一个对表格进行增加和删除
    Vue中的列表渲染
    Vue中的计算属性(computed)和侦听器(watch)
    Vue的模块语法
    vue-cli的搭建
    Vue的概念介绍
    React中函数组件和类组件的区别
  • 原文地址:https://www.cnblogs.com/wangxiaoyong/p/8668395.html
Copyright © 2011-2022 走看看