344. Reverse String【easy】
344. Reverse String【easy】
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 start = , end = s.length() - ; while (start <= end) {
char c = s[start];
s[start] = s[end];
s[end] = c;
++start;
--end;
} return s;
}
};
解法二:
class Solution {
public:
void reverseStringHelper(string & s, int len, int start, string & result)
{
if (start == len) {
return;
} reverseStringHelper(s, len, start + , result); result += s[start];
} string reverseString(string s) {
string result; reverseStringHelper(s, s.length(), , result); return result;
}
};
递归
解法三:
public class Solution {
public String reverseString(String s) {
int length = s.length();
if (length <= ) return s;
String leftStr = s.substring(, length / );
String rightStr = s.substring(length / , length);
return reverseString(rightStr) + reverseString(leftStr);
}
}
参考@ratchapong.t 的代码
Complexity Analysis
Time Complexity: `O(n log(n))` (Average Case) and `O(n * log(n))` (Worst Case) where `n` is the total number character in the input string. The recurrence equation is `T(n) = 2 * T(n/2) + O(n)`. `O(n)` is due to the fact that concatenation function takes linear time. The recurrence equation can be solved to get `O(n * log(n))`.
Auxiliary Space: `O(h)` space is used where `h` is the depth of recursion tree generated which is `log(n)`. Space is needed for activation stack during recursion calls.
Algorithm
Approach: Divide and Conquer (Recursive)
The string is split into half. Each substring will be further divided. This process continues until the string can no longer be divided (length `<= 1`). The conquering process will take they previously split strings and concatenate them in reverse order.
344. Reverse String【easy】的更多相关文章
- 345. Reverse Vowels of a String【easy】
345. Reverse Vowels of a String[easy] Write a function that takes a string as input and reverse only ...
- 53. Reverse Words in a String【easy】
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- 345. Reverse Vowels of a String【Easy】【双指针-反转字符串中的元音字符】
Write a function that takes a string as input and reverse only the vowels of a string. Example 1: In ...
- 413. Reverse Integer【easy】
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- 557. Reverse Words in a String III【easy】
557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...
- 【leetcode】344. Reverse String
problem 344. Reverse String solution: class Solution { public: void reverseString(vector<char> ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
随机推荐
- 【BZOJ3167/4824】[Heoi2013]Sao/[Cqoi2017]老C的键盘
[BZOJ3167][Heoi2013]Sao Description WelcometoSAO(StrangeandAbnormalOnline).这是一个VRMMORPG,含有n个关卡.但是,挑战 ...
- 1.4(学习笔记)JSP自定义标签
一.JSP自定义标签 JSP自定义标签,可以通过实现Tag接口.继承TagSupport类来设置标签功能. 后续通过配置文件将标签和具体的实现类关联. 二.自定义第一个标签(实现Tag接口) 自定义标 ...
- yum出现的“UnicodeDecodeError: 'ascii' codec”问题解决
新装了CentOS 6.5系统,打算使用yum安装程序是出现了如下错误: Loading mirror speeds from cached hostfile Traceback (most rece ...
- 敏捷开发中的sprint是什么意思_百度知道
敏捷开发中的sprint是什么意思_百度知道 敏捷开发中的sprint是什么意思 未成年RB21 | 浏览 4208 次 推荐于2016-02-27 15:19:02 最佳 ...
- Python开发网络爬虫抓取某同城房价信息
前言: 苦逼的我从某某城市换到另一个稍微大点的某某城市,面临的第一个问题就是买房,奋斗10多年,又回到起点,废话就不多说了,看看如何设计程序把某同城上的房价数据抓取过来. 方案:方案思路很简单,先把网 ...
- CSS:超出省略三部曲
overflow:hidden; 超出隐藏 white-space:nowrap; 不让换行,直到<br /> text-overflow:ellipsis; 超出显示省略号... ...
- Hadoop Trash回收站使用指南
转载:https://blog.csdn.net/sunnyyoona/article/details/78869778 我们在删除一个文件时,遇到如下问题,提示我们不能删除文件放回回收站: sudo ...
- 通过ssh上传文件到目标主机
需要通过ssh上传文件到目标主机上,之前一直时通过ssh客户端来传文件的,这次因为本地没装客户端,所以考虑直接用终端通过ssh连接主机进行文件传输. 只需要一条命令就可以了: scp ./serve ...
- 64位系统注冊32位的directshow filter文件
在SERVER2008上注冊自己写的directshow filter 的dll或者ax文件的时候总是提示 [Window Title] RegSvr32 [Content] 模块".\ba ...
- [转载]linux 更新yum源 改成阿里云源
原文链接:https://www.cnblogs.com/bincoding/p/7892762.html 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc ...