Skip to content

Commit cf12236

Browse files
committed
the solution of the problem "Reverse String"
1 parent c38062d commit cf12236

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ LeetCode
1212
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [C++](./algorithms/cpp/intersectionOfTwoArrays/intersectionOfTwoArrays.cpp)|Easy|
1313
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [C++](./algorithms/cpp/topKFrequentElements/topKFrequentElements.cpp)|Medium|
1414
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | [C++](./algorithms/cpp/reverseVowelsOfAString/reverseVowelsOfAString.cpp)|Easy|
15+
|345|[Reverse String](https://leetcode.com/problems/reverse-string/) | [C++](./algorithms/cpp/reverseString/ReverseString.cpp)|Easy|
1516
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/) | [C++](./algorithms/cpp/houseRobber/houseRobberIII.cpp)|Medium|
1617
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [C++](./algorithms/cpp/increasingTripletSubsequence/increasingTripletSubsequence.cpp)|Medium|
1718
|330|[Patching Array](https://leetcode.com/problems/patching-array/) | [C++](./algorithms/cpp/patchingArray/PatchingArray.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Source : https://leetcode.com/problems/reverse-string/
2+
// Author : Hao Chen
3+
// Date : 2016-05-29
4+
5+
/***************************************************************************************
6+
*
7+
* Write a function that takes a string as input and returns the string reversed.
8+
*
9+
* Example:
10+
* Given s = "hello", return "olleh".
11+
***************************************************************************************/
12+
13+
class Solution {
14+
public:
15+
string reverseString(string s) {
16+
int len = s.size();
17+
for (int i=0; i<len/2; i++) {
18+
char ch = s[i];
19+
s[i] = s[len-i-1];
20+
s[len-i-1] = ch;
21+
}
22+
return s;
23+
}
24+
};

0 commit comments

Comments
 (0)