leetcode344——Reverse String(C++)
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
个人博客:http://www.cnblogs.com/wdfwolf3/。
这道题就是简单的字符串逆置,在C++中字符串类型可以作为数组方式处理,所以经典的数组逆置就可以完成。这里提供两种思路:
1.classic方法头尾交换(16ms)
class Solution {
public:
string reverseString(string s) {
int i=,j=s.size()-;
while(i<j)
{
swap(s[i],s[j]);
i++;
j--;
}
return s;
}
};
2.recursion方法分治递归(44ms)
将字符串分为前后两部分,分别逆置然后合并起来,合并时自然是后半部分在前,前半部分在后。用到字符串截取函数string.substr(),它接收两个参数,第一个为位置索引,即截取的开始位置,默认参数为0,就是字符串的开始;第二个参数为截取字符的数目,如果省略,就是指截取到字符串的末尾。
class Solution {
public:
string reverseString(string s) {
int length=s.size();
if(length<)
return s;
return reverseString(s.substr(length/))+reverseString(s.substr(,length/));
}
};
P.S.最后关于swap交换元素介绍两个思路:
1.tmp中间变量,最熟悉的方法。
tmp=i;
i=tmp;
j=tmp;
2.bit的按位抑或^原理,即m^m=0,0^m=m。
m=m&n;
n=m&n; 等价于 n=m&n&n=m;
m=m&n; m=m&n&m=n;
leetcode344——Reverse String(C++)的更多相关文章
- 344. Reverse String(C++)
344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...
- Leetcode#344. Reverse String(反转字符串)
题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...
- Swift2.0 中的String(二):基本操作
Swift中的字符串,第二篇,基本操作.其他的几篇传送门(GitHub打不开链接的同学请自行把地址github改成gitcafe,或者直接去归档里找:-P): Swift2.0 中的String(一) ...
- Swift2.0 中的String(三):类型转换
本系列第三篇,String相关的类型转换.其他的几篇传送门(GitHub打不开链接的同学请自行把地址github改成gitcafe,或者直接去归档里找:-P): Swift2.0 中的String(一 ...
- Swift2.0 中的String(一):常用属性
字符串算是平常用的比较多.花样也比较多的一个类型,昨天有空把相关的一些常用操作都写了一遍,总结出来.其实iOS里面的字符串更复杂,还有NSString系列等等,那些API太多将来需要用的时候再慢慢学. ...
- Java基础知识强化59:String(字符串)和其他类型的相互转化
1. String类型 ---> 其他类型 (1)使用基本类型包装类的parseXXX方法 e.g:String(字符串)转化为int(整型) String MyNumber ="12 ...
- 【CF1132F】Clear the String(动态规划)
[CF1132F]Clear the String(动态规划) 题面 CF 题解 考虑区间\(dp\). 增量考虑,每次考虑最后一个字符和谁一起删去,然后直接转移就行了. #include<io ...
- Redis支持的数据类型及相应操作命令:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sorted set:有序集合)
help 命令,3种形式: help 命令 形式 help @<group> 比如:help @generic.help @string.help @hash.help @list.hel ...
- int和Integer,String和String(包装类)
1.int和Integer的值如果是一样的,则是在内存中开辟相同的内存空间 2.但是String和String(包装类)是不一样的 代码演示: int a=1; Integer b = new Int ...
随机推荐
- HW3.22
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- java nio 快速read大文件
If you want to make your first example faster FileChannel inChannel = new FileInputStream(fileName). ...
- light oj 1148 - Mad Counting
1148 - Mad Counting PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB M ...
- IOS网络多线程-GCD
Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...
- iOS ipv6
这当中最重要的两个概念是DNS64和NAT64. DNS64 DNS64说白了是用来帮助host获取IPv6地址的,传统的DNS服务器可以把域名转换成IPv4地址,但我们的iPhone设备如果处于IP ...
- 微软正式提供Visual Studio 2013正式版下载(附直接链接汇总)
转自 http://www.iruanmi.com/visual-studio-2013/ 微软已经向MSDN订阅用户提供了Visual Studio 2013正式版镜像下载,只是非MSDN用户能够在 ...
- 在chrome下安装Proxy SwitchySharp插件
https://chrome.google.com/webstore/detail/dpplabbmogkhghncfbfdeeokoefdjegm
- insert例子
11.20 使用insert代替下标操作. #include<iostream> #include<map> #include<string> #include&l ...
- QT运行时加载UI文件
写QT程序里运行时加载UI文件,代码如下: 点击(此处)折叠或打开 #include "keyboard.h" #include <QtUiTools> #incl ...
- phpcms 源码分析二:
这次是逆雪寒的common.inc.php第二部分: <?php /* 明天放假了.今天在写点罗.放假没空写了.要陪老婆,大家看了有什么不明白的.可以跟帖问.我懂的我会回答.谢谢 [/php] ...