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:

Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

Example 2:

Input: ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]

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

解法一:

class Solution {
public:
void reverseString(vector<char>& s) {
int left = , right = (int)s.size() - ;
while (left < right) {
char t = s[left];
s[left++] = s[right];
s[right--] = t;
}
}
};

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

解法二:

class Solution {
public:
void reverseString(vector<char>& s) {
int left = , right = (int)s.size() - ;
while (left < right) {
swap(s[left++], s[right--]);
}
}
};

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. 聊一下,前后分离后带来的跨域访问和cookie问题

    在谈前后分离前,我们先看看什么是前后一体的.当我们用javaweb开发网站时,最终我们渲染的jsp或者springthymeleaf.我们的页面其实是WEB-INFO或者templates下.当用户请 ...

  2. 小记 .NET Core 3.0 下 WPF 是如何运行的

    1. 解决方案架构 如图: 2. 生成的代码 如图: /// <summary> /// App /// </summary> public partial class App ...

  3. 大咖云集!Kubernetes and Cloud Native Meetup 深圳站开始报名!

    由阿里技术生态联合 CNCF 官方共同出品的 Kubernetes & Cloud Native Meetup 将在 8 月 31 日来到深圳.届时,阿里云.蚂蚁金服高级技术专家将携手来自国内 ...

  4. 『The Counting Problem 数位dp』

    The Counting Problem Description 求 [L,R]内每个数码出现的次数. Input Format 若干行,一行两个正整数 L 和 R. 最后一行 L=R=0,表示输入结 ...

  5. 在进行机器学习建模时,为什么需要验证集(validation set)?

    在进行机器学习建模时,为什么需要评估集(validation set)? 笔者最近有一篇文章被拒了,其中有一位审稿人提到论文中的一个问题:”应该在验证集上面调整参数,而不是在测试集“.笔者有些不明白为 ...

  6. 2019-11-25-如何在国内发布-UWP-应用

    原文:2019-11-25-如何在国内发布-UWP-应用 title author date CreateTime categories 如何在国内发布 UWP 应用 lindexi 2019-11- ...

  7. C# 协变、逆变

    微软官方概述: 在C#中,协变和逆变能够实现数组类型.委托类型和泛型类型参数的隐式引用转换.协变保留分配兼容性,逆变则与之相反. 协变:能够使用与原始指定的派生类型相比,派生程度更大的类型. 逆变:能 ...

  8. Java生鲜电商平台-redis缓存在商品中的设计与架构

    Java生鲜电商平台-redis缓存在商品中的设计与架构 说明:Java开源生鲜电商平台-redis缓存在商品中的设计与架构. 1. 各种计数,商品维度计数和用户维度计数 说起电商,肯定离不开商品,而 ...

  9. 关于网页布局中常见的margin: 0px ; padding: 0px; 总结

    我们在网页布局中常用到margin: 0px; padding: 0px;  但是在大型的网站布局中通常不这样写通常是按下面这种方式. ul, li, ol, dl, dt, dd, div, p, ...

  10. Kail Linux xface 2019.2

    概述: -OS: Kali-Rolling (2019.2) -DE: XFCE -WM: Arc-Dark -WM Theme: Arc-Dark -Icons: Korla -Term Font: ...