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 = ... 
随机推荐
- [BZOJ3205][APIO2013]Robot(斯坦纳树)
			3205: [Apio2013]机器人 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 1007 Solved: 240[Submit][Status ... 
- CodeForces - 986C  AND Graph
			不难想到,x有边连出的一定是 (2^n-1) ^ x 的一个子集,直接连子集复杂度是爆炸的...但是我们可以一个1一个1的消去,最后变成补集的一个子集. 但是必须当且仅当 至少有一个 a 等于 x 的 ... 
- 【概率dp】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) D. Jon and Orbs
			直接暴力dp就行……f(i,j)表示前i天集齐j种类的可能性.不超过10000天就能满足要求. #include<cstdio> using namespace std; #define ... 
- 对象数组的初始化:null reference
			今天写代码的时候,发现我写的对象数组,只声明,而没有初始化,所以记录一下这个问题:null reference. Animals [] an=new Animals[5];//这只是个对象类型数组的声 ... 
- identifier is too long 异常处理
			修改了oracle中的表. 报 identifier is too long 错误 我执行的脚本是: ---备份create table MDT_AGREEMENTMANAGEMENT_2018080 ... 
- SQL Server Latch Classes Library
			https://www.sqlskills.com/help/latches/ (Companion SQL Server Wait Types Library) This site lists al ... 
- 并发的HashMap为什么会引起死循环?(转)
			本文转自http://blog.csdn.net/zhuqiuhui/article/details/51849692 今天研读Java并发容器和框架时,看到为什么要使用ConcurrentHashM ... 
- java直接跳转页面
			public static String genForwardHtml(String url, Map<String, String> parameters, String charset ... 
- iOS -- 解决iOS11中navigationBar上使用initWithCustomView按钮图片错位 frame无效
			在iOS11上当使用如下代码设置时 UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)]; shareButto ... 
- golang构造单链表
			原文地址:http://www.niu12.com/article/47package main import "fmt" type ListNode struct { Value ... 
