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 = ...
随机推荐
- [LOJ6437]PKUSC
旋转多边形是没有前途的,我们考虑旋转敌人,那么答案就是所有人的可行区间长度之和除以$2\pi$ 首先对每个敌人找到那些旋转后会落到多边形上的角度,实际上就是圆和一些线段求交,解方程即可,注意判一下落在 ...
- 1.2(java学习笔记)类与对象
对象:是指具体的事物.比如学生 张三,老师 李四,代表一个单一的个体. 比如 学生 张三,这时张三就是具体的对象. 类:将对象中共有特征抽象出,可以理解为某种特性的集合. 世界上的事物可以分解成一 ...
- Android获取屏幕的宽度和高度(dp)
public void getAndroiodScreenProperty() { WindowManager wm = (WindowManager) this.getSystemService(C ...
- Java杂谈5——关键字final与volatile
Final关键字 在Java语言中,随着语境的不同final关键字所代表的语义会有一些细微的差异.总的来说,final关键字表达的含义是“禁止修改”,这层有点类似于C++中的const关键字.之所以要 ...
- 大湿教我写程序(2)之走向AV之路
一.大摆庆功宴 上一篇博文<大湿教我写程序(1)之菜单导航篇>中讲到了我撸码到晚上两点多,整出了一个还算是高端大气上档次的demo.半夜回到家里打算着可以好好睡上一个懒觉,到时候直接到客户 ...
- RMAN恢复 增加表空间后控制文件丢失
查看目前的控制文件位置 SQL> select name from v$controlfile; NAME-------------------------------------------- ...
- python 列表合并
列表合并主要有以下方法: 1.用list的extend方法,L1.extend(L2),该方法将参数L2的全部元素添加到L1的尾部 结果:[1, 2, 3, 4, 5, 1, 20, 30] 2.用切 ...
- mysql show profiles使用分析sql性能
mysql show profiles使用分析sql性能 Show profiles是5.0.37之后添加的,要想使用此功能,要确保版本在5.0.37之后. 查看一下我的数据库版本 mysql> ...
- Hadoop Trash回收站使用指南
转载:https://blog.csdn.net/sunnyyoona/article/details/78869778 我们在删除一个文件时,遇到如下问题,提示我们不能删除文件放回回收站: sudo ...
- builder pattern
design patterns 结合书本和这个网站的这个系列的文章来看: https://www.tutorialspoint.com/design_pattern/builder_pattern.h ...