[string]345. Reverse Vowels of a String

345. Reverse Vowels of a String

Given a string s, reverse only all the vowels in the string and return it.

The vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, and they can appear in both cases.

Example 1:

Input: s = “hello”
Output: “holle”

Example 2:

Input: s = “leetcode”
Output: “leotcede”

Constraints:

1 <= s.length <= 3 * 105
s consist of printable ASCII characters.

solution1双指针

class Solution {
public:
    string reverseVowels(string s) {
        auto isVowel = [vowels = "aeiouAEIOU"s](char ch) {
            return vowels.find(ch) != string::npos;
        };

        int n = s.size();
        int i = 0, j = n - 1;
        while (i < j) {
            while (i < n && !isVowel(s[i])) {
                ++i;
            }
            while (j > 0 && !isVowel(s[j])) {
                --j;
            }
            if (i < j) {
                swap(s[i], s[j]);
                ++i;
                --j;
            }
        }
        return s;
    }
};



作者:LeetCode-Solution
链接:https://leetcode.cn/problems/reverse-vowels-of-a-string/solution/fan-zhuan-zi-fu-chuan-zhong-de-yuan-yin-2bmos/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

lambda表达式

ambda 表达式定义了一个匿名函数,并且可以捕获一定范围内的变量。lambda 表达式的语法形式可简单归纳如下:

[ capture ] ( params ) opt -> ret { body; };

其中 capture 是捕获列表,params 是参数表,opt 是函数选项,ret 是返回值类型,body是函数体。

因此,一个完整的 lambda 表达式看起来像这样:

auto f = [](int a) -> int { return a + 1; };
std::cout << f(1) << std::endl;  // 输出: 2

http://c.biancheng.net/view/3741.html

operator " "s

" “s是一种operator,在这里的目的是因为捕获里c++会自动推导变量类型,会把"aeiouAEIOU"推导成const char *const类型,但其实我们需要的是string类型,其实这里”"s就是转成string类型等效于vowels = std::string(“aeiouAEIOU”)。
————————————————
版权声明:本文为CSDN博主「爱菠萝」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013559309/article/details/122554758

string::npos

The constant is the largest representable value of type size_type. It is assuredly larger than max_size(); hence it serves as either a very large value or as a special code.
以上的意思是npos是一个常数,表示size_t的最大值(Maximum value for size_t)。

npos可以表示string的结束位子,是string::type_size 类型的,也就是find()返回的类型。find函数在找不到指定值得情况下会返回string::npos。举例如下(计算字符串中含有的不同字符的个数):
#include
#include
using namespace std;
int main()
{
string b;
getline(cin,b);
int count=0;
for(int i=0;i<=127;i++)
if(b.find(i)!=string::npos)
count++;
cout<<count;

————————————————
版权声明:本文为CSDN博主「木顶思上」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jiejinquanil/article/details/51789682

————————————————
npos is a static member constant value with the greatest possible value for an element of type size_t.
This value, when used as the value for a len (or sublen) parameter in string’s member functions, means "until the end of the string".

As a return value, it is usually used to indicate no matches.

This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值