LeetCode 344. Reverse String

https://leetcode.com/problems/reverse-string/html

Write a function that takes a string as input and returns the string reversed.ios

Example:
Given s = "hello", return "olleh".c++

轉換字符串。string跟char之間的賦值又忘了怎麼弄了。。。dom

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class Solution {
 6 public:
 7     string reverseString(string s) {
 8         int i = 0, j = s.length() - 1;
 9         char* sTemp = (char*)s.c_str();
10         char temp;
11         
12         while (i < j)
13         {
14 //            swap(s[i++], s[j--]);
15             temp = sTemp[i];
16             sTemp[i] = sTemp[j];
17             sTemp[j] = temp;
18             i++;
19             j--;
20         }
21         
22         return (string)sTemp;
23     }
24 };
25 
26 int main ()
27 {
28     Solution testSolution;
29     string result = testSolution.reverseString("hello");    
30     
31     cout << result << endl;
32 
33     char ch;
34     cin >> ch;
35     
36     return 0;
37 }
View Code

swap (string)ide

http://www.cplusplus.com/reference/string/string/swap-free/post

C++中string的用法 string字符串的使用方法
http://jingyan.baidu.com/article/20b68a8854f919796dec6265.htmlurl

標準C++中的string類的用法總結
http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.htmlspa

c++中string的用法
http://www.cnblogs.com/yxnchinahlj/archive/2011/02/12/1952550.html.net

淺析string 與char* char[]之間的轉換
http://www.jb51.net/article/41917.htmcode

LeetCode 344. Reverse String

相關文章
相關標籤/搜索