Article Directory
Reverse string
Title description:
Write a function whose function is to reverse the input string. The input string is given as a character array char [].
Don't allocate extra space to another array, you must modify the input array in place and use O (1) extra space to solve this problem.
You can assume that all characters in the array are printable characters in the ASCII code table.
Example 1:
Input: ["h", "e", "l", "l", "o"]
Output: ["o", "l", "l", "e", "h"]
The solution is as follows:
class Solution {
public:
void reverseString(vector<char>& s) {
int n=s.size();
for(int i = 0; i < n/2; i++)
{
swap(s[i],s[n - 1 - i]);
}
}
};
Reverse vowels in a string
Title description:
Write a function that takes a string as input and reverses the vowels in the string.
Example 1:
Input: "hello"
Output: "holle"
The solution is as follows:
class Solution {
bool isVowel(char ch){
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'
||ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U';
}
public:
string reverseVowels(string s) {
int begin = 0;
int end = s.size() - 1;
while(begin < end){
while(begin != end){
if(isVowel(s[begin]))
break;
++begin;
}
while(begin != end){
if(isVowel(s[end]))
break;
--end;
}
if(begin < end){
swap(s[begin], s[end]);
++begin;
--end;
}
}
return s;
}
};
Reverse String II
Title description:
Given a string and an integer k, you need to reverse the first k characters of every 2k characters from the beginning of the string. If there are less than k characters remaining, all remaining ones are reversed. If there are less than 2k but greater than or equal to k characters, the first k characters are reversed and the remaining characters are left as they are.
Example:
Input: s = "abcdefg", k = 2
Output: "bacdfeg"
Requirements:
The string contains only lowercase English letters.
The length of the given string and k are in the range [1, 10000].
The solution is as follows:
class Solution {
void reverse(string& s, int begin, int end) {
while(begin < end){
swap(s[begin++], s[end--]);
}
}
public:
string reverseStr(string& s, int k) {
int n = s.size();
int start = 0;
while (n >= 2 * k) {
reverse(s, start, start + k - 1);
start += 2 * k;
n -= 2 * k;
}
if (n >= k&& n < 2 * k) {
reverse(s, start, start + k - 1);
}
else if (n < k) {
reverse(s, start, start + n - 1);
}
return s;
}
};
Reverse the word in the string III
Title description:
Given a string, you need to reverse the character order of each word in the string, while still retaining the original order of spaces and words.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by a single space, and there will be no extra spaces in the string.
The solution is as follows:
class Solution {
void reverse(string& s, int begin, int end) {
while (begin < end) {
swap(s[begin++], s[end--]);
}
}
public:
string reverseWords(string s) {
int len = s.size();
int start = 0;
int end = 0;
do
{
end = s.find(' ', end+1);
if (end > 0) {
reverse(s, start, end - 1);
start = end + 1;
}
else {
reverse(s, start, len - 1);
break;
}
} while (start < len);
return s;
}
};