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】的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 413. Reverse Integer【easy】

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  5. 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 ...

  6. 【leetcode】344. Reverse String

    problem 344. Reverse String solution: class Solution { public: void reverseString(vector<char> ...

  7. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  8. 606. Construct String from Binary Tree 【easy】

    606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...

  9. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

随机推荐

  1. Miller-Rabin与Pollard-Rho备忘

    Miller-Rabin素性测试算法: 根据费马小定理当p为素数时成立,所以如果存在一个a使x不满足此定理,则x必然不为素数. 但这是充分条件而不是必要条件,所以对于每个a,可能存在满足定理的x,这时 ...

  2. 【动态规划】UVALive - 4888 - Railroad

    f(i,j)表示从A序列前面取i个,从B序列前面取j个时,能否拼成C序列.转移自行脑补. A train yard is a complex series of railroad tracks for ...

  3. 一个页面如何放多个百度编辑器 Ueditor 1.4.3?PHP如何获取Ueditor 的值?

    问题1:一个页面如何放置多个Ueditor? 参考代码如下: <form  method="post" action="save.php"> < ...

  4. ldr与adr的区别

    参考: http://coon.blogbus.com/logs/2738861.html http://hi.baidu.com/for_guanghui/item/73e60bbcc8be15a2 ...

  5. 【tomcat】FileNotFoundException: C:\Program Files\Java\apache-tomcat-8.5.11-geneshop3\webapps\ROOT\index.html (拒绝访问。)

    新装系统后,tomcat启动起来 提示如下错误: Caused by: java.io.FileNotFoundException: C:\Program Files\Java\apache-tomc ...

  6. t-sql 笔记(2)

    1.用标点符号分隔的字符串,转换成表 -- SELECT * FROM dbo.split('581:579:519:279:406:361:560',':') ), )) )) AS BEGIN D ...

  7. Coherence装载数据的研究 - Invocation Service

    这里验证第三个方法,原理是将需要装载的数据分载在所有的存储节点上,不同的地方是利用了存储节点提供的InvocationService进行装载,而不是PreloadRequest, 原理如图 前提条件是 ...

  8. python socket timeout设置

    需要在调用socket的connect方法之前设置settimeout(time)方法,另外在设置之后要将再次调用settimeout(None)来设置socket进入阻塞模式. 如下代码示例: so ...

  9. 深入理解JavaScript中的函数操作——《JavaScript忍者秘籍》总结

    匿名函数 对于什么是匿名函数,这里就不做过多介绍了.我们需要知道的是,对于JavaScript而言,匿名函数是一个很重要且具有逻辑性的特性.通常,匿名函数的使用情况是:创建一个供以后使用的函数.简单的 ...

  10. PhantomJS用法示例

    收录待用,修改转载已取得腾讯云授权 前言 大家有没有发现之前我们写的爬虫都有一个共性,就是只能爬取单纯的html代码,如果页面是JS渲染的该怎么办呢?如果我们单纯去分析一个个后台的请求,手动去摸索JS ...