Write a function that reverses a string. The input string is given as an array of characters char[].

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

You may assume all the characters consist of printable ascii characters.

Example 1:

  1. Input: ["h","e","l","l","o"]
  2. Output: ["o","l","l","e","h"]

Example 2:

  1. Input: ["H","a","n","n","a","h"]
  2. Output: ["h","a","n","n","a","H"]

这道题没什么难度,直接从两头往中间走,同时交换两边的字符即可,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. void reverseString(vector<char>& s) {
  4. int left = , right = (int)s.size() - ;
  5. while (left < right) {
  6. char t = s[left];
  7. s[left++] = s[right];
  8. s[right--] = t;
  9. }
  10. }
  11. };

我们也可以用 swap 函数来帮助我们翻转:

解法二:

  1. class Solution {
  2. public:
  3. void reverseString(vector<char>& s) {
  4. int left = , right = (int)s.size() - ;
  5. while (left < right) {
  6. swap(s[left++], s[right--]);
  7. }
  8. }
  9. };

Github 同步地址:

https://github.com/grandyang/leetcode/issues/344

类似题目:

Reverse Words in a String II

Reverse Words in a String

Reverse Vowels of a String

Reverse String II

参考资料:

https://leetcode.com/problems/reverse-string/

https://leetcode.com/problems/reverse-string/discuss/80935/Simple-C%2B%2B-solution

https://leetcode.com/problems/reverse-string/discuss/80937/JAVA-Simple-and-Clean-with-Explanations-6-Solutions

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 344. Reverse String 翻转字符串的更多相关文章

  1. LeetCode 344. Reverse String(反转字符串)

    题目描述 LeetCode 344. 反转字符串 请编写一个函数,其功能是将输入的字符串反转过来. 示例 输入: s = "hello" 返回: "olleh" ...

  2. Leetcode#344. Reverse String(反转字符串)

    题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...

  3. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  4. [CareerCup] 1.2 Reverse String 翻转字符串

    1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...

  5. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  6. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  7. Leetcode 344 Reverse String 字符串处理

    题意:反转字符串,用好库函数. class Solution { public: string reverseString(string s) { reverse(s.begin(),s.end()) ...

  8. (字符串 数组 递归 双指针) leetcode 344. Reverse String

    Write a function that reverses a string. The input string is given as an array of characters char[]. ...

  9. 344 Reverse String 反转字符串

    请编写一个函数,其功能是将输入的字符串反转过来.示例:输入:s = "hello"返回:"olleh"详见:https://leetcode.com/probl ...

随机推荐

  1. CompletableFuture1

    public class CompletableFutureTest { public static void main(String[] args) throws Exception { test5 ...

  2. Beats Elastic中的Auditbeat使用介绍

    Auditbeat使用介绍 Auditbeat是一种轻量级的数据收集器,您可以将其安装在服务器上,以审核系统上用户和进程的活动. 例如,您可以使用Auditbeat从Linux Audit Frame ...

  3. 教你使用 Swoole-Tracker 秒级定位 PHP 卡死问题

    PHPer 肯定收到过这样的投诉:小菊花一直在转!你们网站怎么这么卡!当我们线上业务遇到这种卡住(阻塞)的情况,大部分 PHPer 会两眼一抹黑,随后想起那句名言:性能瓶颈都在数据库然后把锅甩给DBA ...

  4. Linux之Shell编程(15)

    case: for:   while:

  5. 「白帽挖洞技能」YxCMS 1.4.7 漏洞分析

    这几天有小伙伴留言给我们,想看一些关于后台的漏洞分析,今天i春秋选择YxCMS 1.4.7版本,理论内容结合实际案例进行深度分析,帮助大家提升挖洞技能. 注:篇幅较长,阅读用时约7分钟. YXcms是 ...

  6. pushad与popad

    版权声明:本文为博主原创文章,转载请附上原文出处链接和本声明.2019-08-24,00:40:12作者By-----溺心与沉浮----博客园 PUSHAD与POPAD 这两条指令其实就是讲EAX,E ...

  7. Linux命令: ps

    STAT 进程状态 S-睡眠 s-进程是会话向导进程 N拥有比普通优先级更低的 R-正在运行 D-短期等待 Z-僵尸进程 T被跟踪或者被停止 STATED 进程启动时间 TIME  进程使用CPU时间 ...

  8. mysql Waiting for table flush

    应用突然被hang住了,tomcat日志报错,所有涉及到数据库的操作都报错,卡死. show processlist 查看到大量的:Waiting for table flush 应该是 进行了 dd ...

  9. Oracle 11g R2 Sample Schemas 安装

    最近准备对之前学习SQL*Loader的笔记进行整理,希望通过官方文档中的示例学习(Case Studies)来进行,但是官方文档中示例学习相关的脚本文件在数据库软件安装完成之后默认并没有提供,而是整 ...

  10. jquery 监听不起效果的小问题汇总

    在写前端页面时,因为我是用jquery添加新的html字符串来实现动态添加.删除,每次新添加都需要生成新的id,当我对新的id进行监听时,却不起作用. 思考了很多方法,开始我以为,如果将监听的语句$( ...