day7打卡

ikun1111發表於2024-08-30
  1. 反轉字串

利用雙指標不斷向中間靠攏, 交換資料

class Solution {
public:
void reverseString(vector& s) {
int left = 0;
int right = s.size() - 1;
while(left < right)
{
char tmp = '\0';
tmp = s[left];
s[left++] = s[right];
s[right--] = tmp;
}

}

};

  1. 反轉字串II

class Solution {
public:
void reverse(string &s, int left, int right)
{
right--;
while(left < right)
{
char tmp = '\0';
tmp = s[left];
s[left++] = s[right];
s[right--] = tmp;
}
}
string reverseStr(string s, int k)
{
for(int i = 0; i < s.size(); i += 2*k)
{
if(i+k <= s.size())
{
reverse(s, i, i + k);
continue;
}
reverse(s, i, s.size());
}
return s;
}
};

替換數字
新建立了一個字串

include

using namespace std;

int main()
{
string s;
while(cin >> s)
{
string ret;
for(int i = 0; i < s.size(); ++i)
{
if(s[i] >= 'a' && s[i] <= 'z')
{
ret += s[i];
}
else if(s[i] >= '0' && s[i] <= '9')
{
ret += "number";
}
}
cout << ret << endl;
}
}

相關文章