the solution of the problem "Reverse String"

This commit is contained in:
Hao Chen 2016-05-29 15:37:31 +08:00
parent 2cdbde2580
commit 705813803d
2 changed files with 25 additions and 0 deletions

View File

@ -12,6 +12,7 @@ LeetCode
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [C++](./algorithms/cpp/intersectionOfTwoArrays/intersectionOfTwoArrays.cpp)|Easy|
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [C++](./algorithms/cpp/topKFrequentElements/topKFrequentElements.cpp)|Medium|
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | [C++](./algorithms/cpp/reverseVowelsOfAString/reverseVowelsOfAString.cpp)|Easy|
|345|[Reverse String](https://leetcode.com/problems/reverse-string/) | [C++](./algorithms/cpp/reverseString/ReverseString.cpp)|Easy|
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/) | [C++](./algorithms/cpp/houseRobber/houseRobberIII.cpp)|Medium|
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [C++](./algorithms/cpp/increasingTripletSubsequence/increasingTripletSubsequence.cpp)|Medium|
|330|[Patching Array](https://leetcode.com/problems/patching-array/) | [C++](./algorithms/cpp/patchingArray/PatchingArray.cpp)|Medium|

View File

@ -0,0 +1,24 @@
// Source : https://leetcode.com/problems/reverse-string/
// Author : Hao Chen
// Date : 2016-05-29
/***************************************************************************************
*
* Write a function that takes a string as input and returns the string reversed.
*
* Example:
* Given s = "hello", return "olleh".
***************************************************************************************/
class Solution {
public:
string reverseString(string s) {
int len = s.size();
for (int i=0; i<len/2; i++) {
char ch = s[i];
s[i] = s[len-i-1];
s[len-i-1] = ch;
}
return s;
}
};