leetcode 344
2016-05-02 18:07
399 查看
ResverString
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Subscribe to see which companies asked this question
C++
1、STL
class Solution {
public:
string reverseString(string s) {
reverse(s.begin(),s.end());
return s;
}
};
2、swap in-place
class Solution {
public:
string reverseString(string s) {
auto i=0;
auto j=s.size();
if(j==0)
return s;
else
j--;
while(i<j){
swap(s[i++],s[j--]);
}
return s;
}
};
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Subscribe to see which companies asked this question
C++
1、STL
class Solution {
public:
string reverseString(string s) {
reverse(s.begin(),s.end());
return s;
}
};
2、swap in-place
class Solution {
public:
string reverseString(string s) {
auto i=0;
auto j=s.size();
if(j==0)
return s;
else
j--;
while(i<j){
swap(s[i++],s[j--]);
}
return s;
}
};
相关文章推荐
- ActiveMQ实战(二)-代码示例
- POJ-2387Til the Cows Come Home,最短路坑题,dijkstra注意一下重边就过了~~
- tomcat 中的 bootstrap 与 catalina
- cocoa pods的安装
- scrollView 实现图片轮播
- 数据结构之排序:堆排序
- hdu 5675 ztr loves math(数学技巧)
- Design Pattern - Behavioral Patterns - Mediator Pattern
- 自定义view实现ViewPageIndicator(viewpage指示器) so easy
- POJ3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- java动态代理(JDK和cglib)
- 拿来主义太严重
- opencv、matlab查看函数的定义、封装
- php实现堆排序
- 简易时钟
- Nginx之location匹配规则(个人总结)
- 如果一个技术博客的作者,没有勇气自建一个博客,为什么呢?
- C++作业四
- 安装Visual Studio 2013 出现0x80070643错误