344. Reverse String(C++)
344. Reverse String
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
题目大意:
字符串倒置。
解题方法:
第一个字符与最后一个非空字符对换。
注意事项:
1.字符串最后一个字符是空字符。
C++代码:
1.不良代码:
class Solution {
public:
string reverseString(string s) {
char *f,*e;
char temp;
f=&s[];
e=&s[s.length()-];
while(f!=e&f!=&s[s.length()/])
{
temp=*f;
*f=*e;
*e=temp;
f++;
e--;
}
return s;
}
};
2.改进后的代码:
class Solution {
public:
string reverseString(string s) {
for(int i=,j=s.size()-;i<j&&i!=j;i++,j--)
{
swap(s[i],s[j]);
}
return s;
}
};
344. Reverse String(C++)的更多相关文章
- Leetcode#344. Reverse String(反转字符串)
题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...
- leetcode344——Reverse String(C++)
Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- 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) { ...
- Swift2.0 中的String(二):基本操作
Swift中的字符串,第二篇,基本操作.其他的几篇传送门(GitHub打不开链接的同学请自行把地址github改成gitcafe,或者直接去归档里找:-P): Swift2.0 中的String(一) ...
- Swift2.0 中的String(三):类型转换
本系列第三篇,String相关的类型转换.其他的几篇传送门(GitHub打不开链接的同学请自行把地址github改成gitcafe,或者直接去归档里找:-P): Swift2.0 中的String(一 ...
- Swift2.0 中的String(一):常用属性
字符串算是平常用的比较多.花样也比较多的一个类型,昨天有空把相关的一些常用操作都写了一遍,总结出来.其实iOS里面的字符串更复杂,还有NSString系列等等,那些API太多将来需要用的时候再慢慢学. ...
- Java基础知识强化59:String(字符串)和其他类型的相互转化
1. String类型 ---> 其他类型 (1)使用基本类型包装类的parseXXX方法 e.g:String(字符串)转化为int(整型) String MyNumber ="12 ...
- 【CF1132F】Clear the String(动态规划)
[CF1132F]Clear the String(动态规划) 题面 CF 题解 考虑区间\(dp\). 增量考虑,每次考虑最后一个字符和谁一起删去,然后直接转移就行了. #include<io ...
随机推荐
- 【Mongous】write after end
执行1(---) 执行2(----) 完成1(POST) 执行3(---)
- 导入 from pdfminer.pdfinterp import process_pdf 错误
>>> from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter>>> from ...
- vijosP1195“非常男女”计划
vijosP1195“非常男女”计划 链接:https://vijos.org/p/1195 [思路] 人数差. 人数差相等的两点之间的区间一定有男女人数相等. 计目前为止到i的1为sum1,0为su ...
- C# Dictionary的xml序列化
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- kickStart脚本
kickstart是什么 许多系统管理员宁愿使用自动化的安装方法来安装红帽企业 Linux.为了满足这种需要,红帽创建了kickstart安装方法.使用kickstart,系统管理员可以 ...
- CSS3新特性(阴影、动画、渐变、变形、伪元素等)
CSS3与页面布局学习总结(六)--CSS3新特性(阴影.动画.渐变.变形.伪元素等) 目录 一.阴影 1.1.文字阴影 1.2.盒子阴影 二.背景 2.1.背景图像尺寸 2.2.背景图像显示的原 ...
- 用ChooseALicense帮自己选一个开源license,然后用AddALicense给自己的github自动加上license文件
在我之前的一篇博客里面介绍过tl;drLegal ——开源软件license的搜索引擎,可以很方便的查询各种license,并且给出了很简洁的解释.今天又发现了另外一个帮助你选择你的开源软件licen ...
- 在LaTeX里插入全页的pdf 分类: LaTex 2015-02-04 17:20 142人阅读 评论(0) 收藏
要帮女友排版毕业论文,需要插入封面,省时省力的方法就是把学校给的Word封面保存成PDF然后插入到Latex文档中. 首先添加下面的宏: \usepackage[final]{pdfpages} 然后 ...
- [RxJS] What RxJS operators are
We have covered the basics of what is Observable.create, and other creation functions. Now lets fina ...
- xslt语法之---position()函数
最近在学习使用XSLT,很好很强大的样式表语言.使用到了position()函数特此记录一下. position()函数--返回节点位置 语法:position() 参数:无 返回值:整数 用途:该函 ...