Leetcode刷题19-344.反转字符串(C++)
2019-03-11 15:34
274 查看
题目来源:链接: [https://leetcode-cn.com/problems/reverse-string/].
344.反转字符串
1.问题描述
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。
不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。
示例1:
输入:["h","e","l","l","o"] 输出:["o","l","l","e","h"]
示例2:
输入:["H","a","n","n","a","h"] 输出:["h","a","n","n","a","H"]
2.我的解决方案
很容易的字符反转,就不细说了。
代码如下:
class Solution { public: void reverseString(vector<char>& s) { char tmp; int min = 0; int max = s.size(); int len = max/2; for(int i = 0; i < len; i++) { tmp = s[min]; s[min] = s[max-1]; s[max-1] = tmp; min++; max--; } } };
执行用时 : 108 ms, 在Reverse String的C++提交中击败了1.41% 的用户 内存消耗 : 17.8 MB, 在Reverse String的C++提交中击败了0.84% 的用户
3.大神们的解决方案
。。。面试的时候不能这么干吧。。。
class Solution { public: void reverseString(vector<char>& s) { reverse(s.begin(),s.end()); } };
4.我的收获
hahaha
2019/3/11 胡云层 于南京 19
相关文章推荐
- leetcode-344. 反转字符串[python]
- Leetcode刷题29-557.反转字符串中的单词 III(C++)
- leetcode 反转字符串 (C++实现)
- LeetCode344.python实现: 反转字符串问题☆
- 助教:c/c++——字符串反转及字符串连接:区分gets,puts,scanf(),printf()
- 反转字符串(c++实现)
- LeetCode | Reverse Words in a String(字符串中的单词序反转)
- C++实现字符串反转
- 字符串反转C++实现源码(带测试用例)
- c++中字符串反转的3种方法<转>
- leetcode 206. Reverse Linked List 反转字符串
- [leetcode, python] Reverse Words in a String 反转字符串
- 字符串反转-C++
- c++中字符串反转的3种方法
- 字符串反转 reverse-words-in-a-string @LeetCode
- c++中字符串反转的3种方法
- [leetcode]反转字符串[javascript]
- C/C++面试程序题(一)——字符串反转、链表反转的递归、非递归实现
- 557.反转字符串中的单词 III(C++)
- [leetcode, python] Reverse Words in a String 反转字符串