[LeetCode] 344. Reverse String 翻转字符串
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
类似题目:
参考资料:
https://leetcode.com/problems/reverse-string/
https://leetcode.com/problems/reverse-string/discuss/80935/Simple-C%2B%2B-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 344. Reverse String 翻转字符串的更多相关文章
- LeetCode 344. Reverse String(反转字符串)
题目描述 LeetCode 344. 反转字符串 请编写一个函数,其功能是将输入的字符串反转过来. 示例 输入: s = "hello" 返回: "olleh" ...
- Leetcode#344. Reverse String(反转字符串)
题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- [CareerCup] 1.2 Reverse String 翻转字符串
1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- Leetcode 344 Reverse String 字符串处理
题意:反转字符串,用好库函数. class Solution { public: string reverseString(string s) { reverse(s.begin(),s.end()) ...
- (字符串 数组 递归 双指针) leetcode 344. Reverse String
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
- 344 Reverse String 反转字符串
请编写一个函数,其功能是将输入的字符串反转过来.示例:输入:s = "hello"返回:"olleh"详见:https://leetcode.com/probl ...
随机推荐
- python-5-str常用操作
前言 本节将讲解的是字符串 str 常用的操作方法,与 for 循环. 一.srt 常用操作 1.首个字母大写: # 1.首个字母大写 s = 'xiao long' s1 = s.capitaliz ...
- LeetCode 2:两数相加 Add Two Numbers
给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...
- 【大数据】SparkSql 连接查询中的谓词下推处理 (二)
本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/II48YxGfoursKVvdAXYbVg作者:李勇 目录:1.左表 join 后条件下推2.左表j ...
- Java单元测试简述
最开始项目中是没有单元测试的,基本都是自己通过各种方式来实现测试的.比如修改代码,测完再改回来:再比如直接模拟用户操作,直接当黑盒测试,然后自己去看相应的逻辑有没有,状态有没有改变. 这些方式有几个缺 ...
- Java JDK和IntelliJ IDEA 配置及安装
序言 初学java,idea走一波先.安装完成,配置配置项. idea 软件 官方下载地址:https://www.jetbrains.com/idea/download/#section=windo ...
- MySQL for OPS 04:存储引擎
写在前面的话 在使用 Linux 的时候,可以经常听到有关文件系统 FS(File System)的东西,MySQL 也有属于自己类似的东西,那就是存储引擎.之前在创建数据表的时候,在 Create ...
- Tomcat 简单容器化
Tomcat 容器化 思考 问题1 , Tomcat 容器化,Tomcat 如何配置 APR 连接器 Tomcat 的基础镜像已经是开启了 APR. 问题2, Tomcat 是每次都需要重新构建. 一 ...
- My time is limited
Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma - whic ...
- Python requests库的使用(一)
requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.python-requests.org/z ...
- 剑指 Offer——13. 调整数组顺序使奇数位于偶数前面
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变.这和书本不太一样. 解 ...