(字符串 数组 递归 双指针) 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"]
------------------------------------------------------------------------------------------------------------------------------------------------------
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的更多相关文章
- 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 ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- LeetCode 344. Reverse String(反转字符串)
题目描述 LeetCode 344. 反转字符串 请编写一个函数,其功能是将输入的字符串反转过来. 示例 输入: s = "hello" 返回: "olleh" ...
- [LeetCode] 344. Reverse String 翻转字符串
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
- Leetcode 344 Reverse String 字符串处理
题意:反转字符串,用好库函数. class Solution { public: string reverseString(string s) { reverse(s.begin(),s.end()) ...
- LeetCode 344. Reverse String
Problem: Write a function that takes a string as input and returns the string reversed. Example: Giv ...
- Python [Leetcode 344]Reverse String
题目描述: Write a function that takes a string as input and returns the string reversed. Example:Given s ...
- 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) { ...
随机推荐
- 【腾讯云服务器】基于centos7搭建ftp服务器(vsftpd)
该博客分为三部分设置,1.ftp服务器搭建.2.防火墙设置 3.腾讯云安全组 一.ftp服务器搭建 1.1 安装vsftpd yum install vsftpd -y 1.2 启动vsftpd服 ...
- Linux 系统进程相关命令
1.pstree :可以使用pstree命令来查看系统中进程的分布结构. 2.ps: 常用于查看系统进程的命令是ps(process status)命令,可通过它来查看系统进程的最基本信息. ●-A ...
- 黑洞有毛 or 黑洞无毛:4星|《环球科学》2019年03月号
<环球科学>2019年03月号 高水平的科普杂志.本期我感兴趣的话题有: 1:65岁以上老年人是转发假新闻的主力: 2:人的面孔特征可以通过50个维度来定义: 3:华裔科学家发现人脑颞叶中 ...
- js字符串String提取方法比较
JavaScript: Slice, Substring, or Substr的选择! 在JavaScript中,字符串主要通过以下String方法之一提取: // slice // syntax: ...
- Python安装包:协程(gevent)
- TableExistsException: hbase:namespace
解决:zookeeper还保留着上一次的Hbase设置,所以造成了冲突.删除zookeeper信息,重启之后就没问题了 1.切换到zookeeper的bin目录: 2.执行$sh zkCli.sh 输 ...
- nohup ./startWebLogic.sh >out.log 2>&1 & 解析
在启动weblogic的时候我们经常看到如下的命令: nohup ./startWebLogic.sh >out.log 2>&1 & 从09年开始用weblogic到现在 ...
- laravel学习笔记一
指定端口 数据迁移 php artisan migrate:install 任何路由 match get,post只选择其一 没有表名对应默认的posts表,如果表为post就不行 时区不对时 分页 ...
- iOS开发基础-图片切换(3)之属性列表
延续:iOS开发基础-图片切换(2),对(2)里面的代码用属性列表plist进行改善. 新建 Property List 命名为 Data 获得一个后缀为 .plist 的文件. 按如图修改刚创建的文 ...
- typeScript面对对象篇一
面向对象是typescript的核心部分,这里先介绍下面向对象的七大原则: 单一原则:一个类子负责一个职责. 里氏替换原则:子类可以在任何地方替换它的父类. 依赖倒置原则:代码要依赖于抽象的类,而不要 ...