博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 344. Reverse String
阅读量:7071 次
发布时间:2019-06-28

本文共 1425 字,大约阅读时间需要 4 分钟。

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

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

Example:

Given s = "hello", return "olleh".

转换字符串。string跟char之间的赋值又忘了怎么弄了。。。

1 #include 
2 #include
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)

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

C++中string的用法 string字符串的使用方法

http://jingyan.baidu.com/article/20b68a8854f919796dec6265.html

标准C++中的string类的用法总结

http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html

c++中string的用法

http://www.cnblogs.com/yxnchinahlj/archive/2011/02/12/1952550.html

浅析string 与char* char[]之间的转换

http://www.jb51.net/article/41917.htm

转载于:https://www.cnblogs.com/pegasus923/p/5496001.html

你可能感兴趣的文章
SQL XML 2
查看>>
关于jetty和webx对于HttpServletResponse getWriter和getOutputStream的处理
查看>>
纯CSS3写的10个不同的酷炫图片遮罩层效果
查看>>
关于电脑BIOS解密,如何解密问题?
查看>>
Spring线程安全的实现机制--ThreadLocal
查看>>
html5+canvs实现flash效果。
查看>>
form-data、x-www-form-urlencoded、raw、binary的区别
查看>>
第1周---python网络爬虫规则
查看>>
Shell中cut用法
查看>>
Python 文件操作
查看>>
AC620教程 第十五节 8位7段数码管驱动设计与验证
查看>>
第十章 Libgdx音频之概述
查看>>
【CSS】使用CSS控制文字过多自动省略号
查看>>
【maven】maven创建web项目-pom文件提示web.xml is missing and <failOnMissingWebXml> is set to true...
查看>>
2016数据库考试题
查看>>
使用Zipalign工具优化Android APK应用记录
查看>>
LinearLayout属性baselineAligned的作用及baseline
查看>>
事件怎么看?
查看>>
css页面字体替换源代码和页面显示不一样问题解决
查看>>
读书笔记--精通CSS高级Web标准解决方案(五)---链接样式
查看>>