-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse String.cpp
More file actions
51 lines (44 loc) · 908 Bytes
/
Reverse String.cpp
File metadata and controls
51 lines (44 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution {
public:
void reverseString(vector<char>& s) {
int low=0;
int high=s.size()-1;
while(low<=high)
{
swap(s[low],s[high]);
low++;
high--;
}
for(int i=0;i<s.size();i++)
{
cout<<s[i]<<" ";
}
}
};
//Another approches below
class Solution {
public:
string reverseString(string s) {
int n = s.size();
for(int i = 0; i < n/2; i++) {
swap(s[i], s[n - 1 - i]);
}
return s;
}
};
///////////////////
class Solution {
public:
string reverseString(string s) {
return { s.rbegin(), s.rend() };
}
};
////////////////////
string reverseString(string s) {
int length= s.size();
string str;
for(int i=length-1; i>=0; i--){
str+=(char) s[i];
}
return str;
}