[LeetCode]344. Reverse String
来源:互联网 发布:用手机淘宝买东西步骤 编辑:程序博客网 时间:2024/06/12 03:10
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
题目不难,但是超了time limit
StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象,在内存使用上要优于String类。
所以在实际使用时,如果经常需要对一个字符串进行修改,例如插入、删除等操作,使用StringBuffer要更加适合一些。
在StringBuffer类中存在很多和String类一样的方法,这些方法在功能上和String类中的功能是完全一样的。
但是有一个最显著的区别在于,对于StringBuffer对象的每次修改都会改变对象自身,这点是和String类最大的区别。
StringBuffer详解
public static String reverseString3(String s) { return new StringBuffer(s).reverse().toString();}
还有一种,相比我自己写的O(n),是O(n/2),用首尾交换。
public static String reverseString4(String s) { char[] ch = s.toCharArray(); int halfLength = s.length() / 2; char temp; for (int i = 0; i < halfLength; i++) { temp = ch[s.length() - 1 - i]; ch[s.length() - 1 - i] = ch[i]; ch[i] = temp; } return new String(ch); }
阅读全文
0 0
- leetcode--344. Reverse String
- LeetCode 344. Reverse String
- LeetCode 344. Reverse String
- Leetcode:344. Reverse String
- LeetCode *** 344. Reverse String
- LeetCode *** 344. Reverse String
- [leetcode] 344. Reverse String
- <LeetCode>344. Reverse String
- Leetcode.344. Reverse String
- LeetCode-344.Reverse String
- [leetcode]344. Reverse String
- Leetcode 344. Reverse String
- [leetcode] 344. Reverse String
- [Leetcode]344. Reverse String
- LeetCode - 344. Reverse String
- [leetcode]344. Reverse String
- LeetCode 344. Reverse String
- 【Leetcode】344. Reverse String
- 四则运算
- TextView使用
- MemCache超详细解读
- IIS7 Manager在任务栏中不能正常打开
- 关于PreparedStatement以及Jpa中in参数的设置
- [LeetCode]344. Reverse String
- Android提醒,Dialog、Toast和Snackbar。
- DevExpress.XtraEditors.CheckedListBoxControl 控件使用
- Static修饰符--简单讲解
- thinkphp框架操作mongo
- ios中UIButton的使用
- 教你上传本地代码到github
- 综合业务分析高并发及解决方案
- json与jsonp跨域(一)