Leet Code OJ 344. Reverse String [Difficulty: Easy]
2016-06-23 13:50
435 查看
题目:
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
翻译:
写一个函数,使用字符串作为输入,返回它反转后的结果。
例如,输入”hello”,返回”olleh”。
分析:
转为字符数组后,将第一个字符和最后一个字符对调,第二个字符和倒数第二个对调,以此类推。
Java版代码(时间复杂度O(n),空间复杂度O(n)):
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
翻译:
写一个函数,使用字符串作为输入,返回它反转后的结果。
例如,输入”hello”,返回”olleh”。
分析:
转为字符数组后,将第一个字符和最后一个字符对调,第二个字符和倒数第二个对调,以此类推。
Java版代码(时间复杂度O(n),空间复杂度O(n)):
public class Solution { public String reverseString(String s) { char[] chars=s.toCharArray(); int len=chars.length; char temp; for(int i=0;i<len/2;i++){ temp=chars[i]; chars[i]=chars[len-1-i]; chars[len-1-i]=temp; } return new String(chars); } }
相关文章推荐
- ado完整的增删改查操作
- PHP获取远程图片并调整图像大小(转)
- jq获取当前点击的li是ul中的第几个?
- 序列不存在
- Linux如何更改VirtualBox虚拟机的磁盘容量--gparted合并分区(不能扩展主分区不足点,需要配置LiveCD使用)
- Ubuntu修复grub引导
- I.MX6Q(TQIMX6Q/TQE9)学习笔记——新版BSP之网卡驱动移植
- top命令学习
- Ryan的OC学习总结-----1 第一个OC程序
- Linux 下安装 Blackfin 交叉编译工具链
- 关于JavaScript的类的继承
- 数据库系统异常排查之DMV(转)
- Java的Integer与int互转
- C++读写excel文件(三)—— 用OLE读写
- oracle查找重复记录
- 欢迎使用CSDN-markdown编辑器
- 通过inputSplit分片size控制map数目
- Cloud Engine:大杀器如何炼成
- 进度条15
- js 各种高度宽度汇总(为自己记忆)