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. 【二分答案】【Heap-Dijkstra】bzoj2709 [Violet 1]迷宫花园

    显然最短路长度随着v的变化是单调的,于是可以二分答案,据说spfa在网格图上表现较差. #include<cstdio> #include<cstring> #include& ...

  2. [CF842E]Nikita and game

    [CF842E]Nikita and game 题目链接: CF842E 博客地址: [CF842E]Nikita and game - skylee 题目大意: 一棵树初始只有一个编号为\(1\)的 ...

  3. javascript模块化有什么意义?

    以前,所有的javascript都写在一个文件里,方便只加载一个文件就可以了,但是代码越来越多,必须分成多个文件加载,类似: <script src="1.js">&l ...

  4. golangWEB框架gin学习之获取get参数

    原文地址:http://www.niu12.com/article/40package main import ( "fmt" "github.com/gin-gonic ...

  5. Android Design Support Library介绍之:环境搭建

    在2015年的GoogleIO大会上.具体的Material Design设计规范出炉了.全新的Android Design Support Library 格.更让人开心的是,这些很酷的风格能够通过 ...

  6. SpringMVC学习记录(七)--拦截器的使用

    SpringMVC的请求如以下这样的图所看到的: 能够看出全部的请求都要通过Dispatherservlet来接收,然后通过Handlermapping来决定使用哪个控制器,再依据ViewResolv ...

  7. 【OpenStack 虚拟机初始化user-data & Cloud-init】

    示例: import httplib import json import base64 tenant_id='xxx' token='xxx' compute_host="xxx" ...

  8. 如何在不重启或重新格式化hadoop集群的情况下删除集群节点

    在master节点上的hadoop安装目录下 进入conf目录 配置hdfs-site.xml文件 添加节点如下: <property> <name>dfs.hosts.exc ...

  9. MongoDB集群设置集合分片生效及查看集合分片情况

    #连接mongos /opt/mongodb/mongodb-linux-x86_64-2.4.8/bin/mongo  127.0.0.1:27017   #使用admin数据库 use admin ...

  10. 大话JS神器之Promise

    前段时间的工作中,由于项目要在前端实现存储,于是便使用了websql,而websql的API涉及到了很多的异步问题,如果采取回调函数的方式处理,代码不够优雅,而且不利于理解,于是便找到了Promise ...