the solution of the problem "Reverse String"
This commit is contained in:
parent
2cdbde2580
commit
705813803d
@ -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|
|
||||
|
24
algorithms/cpp/reverseString/ReverseString.cpp
Normal file
24
algorithms/cpp/reverseString/ReverseString.cpp
Normal 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;
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user