c++双指针leetcode344、557
作者:互联网
344. 反转字符串 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public:
void reverseString(vector<char>& s) {
int left=0,right=s.size()-1;
while(left<=right)
{
char temp=s[left];
s[left]=s[right];
s[right]=temp;
left++;
right--;
}
}
};
557. 反转字符串中的单词 III - 力扣(LeetCode) (leetcode-cn.com)
套用了上一题的反转单词作为子函数
class Solution {
public:
void revers(string&s,int left,int right)
{
while(left<=right)
{
char temp=s[left];
s[left]=s[right];
s[right]=temp;
left++;
right--;
}
}
string reverseWords(string s) {
int left=0;
int right=0;
while(right<=s.size())
{
if(s[right]==' '||right==s.size()){
revers(s,left,right-1);
left=right+1;
}
right++;
}
return s;
}
};
标签:right,cn,int,反转,557,c++,leetcode344,left 来源: https://blog.csdn.net/weixin_42153964/article/details/121513170