[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 ...
随机推荐
- 微软宣布成立.NET基金会全面支持开源项目 包括C#编译器Roslyn【转】
基金会初始董事包括 Mono 项目和 Xamarin 的老大 Miguel de Icaza,微软 .NET 团队代表和微软开放技术公司(这是微软专门为开源和开放技术.标准化成立的独立公司)代表. 首 ...
- 一个简单的利用 WebClient 异步下载的示例(一)
继上一篇文章 一个简单的利用 HttpClient 异步下载的示例 ,我们知道不管是 HttpClient,还算 WebClient,都不建议每次调用都 new HttpClient,或 new We ...
- Linux SELinux 使用操作
Linux SELinux 使用操作 # 修改 SELinux 启动模式.临时生效 命令:setenforce [0|1] 0:转成 permissive 宽容模式: 1:转成 Enforcing 强 ...
- Binding ,抄自 http://www.cnblogs.com/cnblogsfans/archive/2011/02/19/1958586.html
1. 绑定到其它元素 <Grid> <StackPanel > <TextBox x:Name="textBox1" Height="150 ...
- C#循环结构
一.背景: 因编程的基础差,因此最近开始巩固学习C#基础,后期把自己学习的东西,总结相应文章中,有不足处请大家多多指教. 二.简介 有的时候,可能需要多次执行同一块代码.一般情况下,语句是顺序执行的: ...
- 基于OpenCV.Net投影法进行文本分块切割
假设有如下一张图,如何把其中的文本分块切割出来,比如“华普超市朝阳门店”.“2015-07-26”就是两个文本块. 做图像切割有很多种方法,本文描述一种最直观的投影检测法.先来看看什么是投影,简单来说 ...
- Ansible Jinja2 模板
1.jinja2渲染NginxProxy配置文件 jinja2 房屋建筑设计固定的? jinja2模板与Ansible关系 Ansible如何使用jinja2模板 template模块 拷贝文件? t ...
- 解决eclipse中maven多模块项目显示不全的问题
背景:在eclipse中导入maven项目,后来发现有的子模块不能正常的显示出现 原因:没有加载到子模块的pom文件 解决方法:重新导入:import-> 勾选项目->选择你缺少的项目的p ...
- linux wake on lan功能通过ethtool配置【转】
转自:https://blog.csdn.net/fanlilei/article/details/38042063 ethtool工具中的wol功能一直很迷惑.今天看了代码将其帮助中下面的参数说明下 ...
- Go语言goroutine调度器初始化(12)
本文是<Go语言调度器源代码情景分析>系列的第12篇,也是第二章的第2小节. 本章将以下面这个简单的Hello World程序为例,通过跟踪其从启动到退出这一完整的运行流程来分析Go语言调 ...