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"]

------------------------------------------------------------------------------------------------------------------------------------------------------

1)
这个是水题,不过我打算用递归来解决它
C++代码:
class Solution {
public:
void reverseString(vector<char>& s) {
helper(s,0,s.size()-1);
}
void helper(vector<char>& s,int start,int end){
if(start >= end){
return;
}
char tmp = s[start];
s[start] = s[end];
s[end] = tmp;
helper(s,start+1,end-1); //是两端往中心靠的递归
}
};

2)

这个可以用双指针。

C++代码:

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

(字符串 数组 递归 双指针) leetcode 344. Reverse String的更多相关文章

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

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

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

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

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

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

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

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

  5. [LeetCode] 344. Reverse String 翻转字符串

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

  6. Leetcode 344 Reverse String 字符串处理

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

  7. LeetCode 344. Reverse String

    Problem: Write a function that takes a string as input and returns the string reversed. Example: Giv ...

  8. Python [Leetcode 344]Reverse String

    题目描述: Write a function that takes a string as input and returns the string reversed. Example:Given s ...

  9. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

随机推荐

  1. KASAN实现原理【转】

    1. 前言 KASAN是一个动态检测内存错误的工具.KASAN可以检测全局变量.栈.堆分配的内存发生越界访问等问题.功能比SLUB DEBUG齐全并且支持实时检测.越界访问的严重性和危害性通过我之前的 ...

  2. AXI-Lite总线及其自定义IP核使用分析总结

    ZYNQ的优势在于通过高效的接口总线组成了ARM+FPGA的架构.我认为两者是互为底层的,当进行算法验证时,ARM端现有的硬件控制器和库函数可以很方便地连接外设,而不像FPGA设计那样完全写出接口时序 ...

  3. Vim 宏

    宏的概念 什么是宏呢?英文名:macro,代表一串命令的集合. 示例操作文本 SELECT * FROM `edu_ocr_task` WHERE ((`userId`=284871) AND (`u ...

  4. 【Linux基础】查看硬件信息-系统

    1.查看计算机名 hostname 2.查看内核/操作系统/CPU信息 uname -a   4.查看操作系统版本(Linux) head -n 2 /etc/issue Red Hat Enterp ...

  5. Python开发【第三篇】基本数据类型

    整型 int __author__ = 'Tang' # 将字符串转换为数字 a = " b = int(a) # 前面是0的数转换,默认base按照十进制 a = " b = i ...

  6. github使用个人总结

    1.获取github上面的源码时候,不能获取最新的,因为你的开发工作不一定是最新的要下载历史版本. 2.要使用里面的文件的时候,可以在目录后面url后面添加downloads 这样可以找到封装好的版本 ...

  7. python3 pickle模块

    import pickle '''将对象转化为硬盘能识别的bytes的过程被称为序列号将bytes转化为对象的过程被称为反序列化'''lst = ["苹果", "橘子&q ...

  8. MySQL数据库事务及其特性

    一.事务概念 事务就是一个程序执行单元,里面的操作要么都做,要么都不做. 二.事务特性 事务有四个非常重要的特性(ACID): 原子性(Atomicity):事务是不可分割的整体,所有操作要么全做,要 ...

  9. C++ SIMD

    SIMD Single Instruction Multiple Data

  10. android H5支付 网络环境未能通过安全验证,请稍后再试

    android做混合开发微信H5支付时碰到的一个问题. 解决办法:把所使用的WebView中重新如下方法即可 webView.setWebViewClient(new WebViewClient() ...