114.Reverse String
来源:互联网 发布:数据字典下载 编辑:程序博客网 时间:2024/06/02 05:31
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
分析:
首先把字符串转化为字符数组,然后转置字符数组,然后再转化为字符串。不直接交换字符串是因为字符串是不可变的,每次改变字符串则会生成一个新的自字符串对象,影响性能。
/**@author * 给定一个字符串对其进行逆转。 * @date 20160423 * @param s * @return */public String reverseString(String s) {char[] arr = s.toCharArray();int len = s.length();if(len<=1){return s;}char temp;for(int i=0;i<len/2;i++){temp = arr[i];arr[i] = arr[len-i-1];arr[len-i-1] = temp;} return new String(arr); }
0 0
- 114.Reverse String
- Reverse string
- String Reverse
- reverse string
- string reverse
- reverse string
- reverse string
- Reverse String
- reverse string
- Reverse String
- Reverse String
- Reverse String
- Reverse String
- Reverse String
- Reverse String
- Reverse String
- Reverse String
- reverse string
- NSURLSession如何取消请求
- ruby on rails开发环境安装血泪史
- Netmap分析(二)
- [Contest] Sdoi2016 题目汇总
- vagrant up and authentication failure
- 114.Reverse String
- {Less} is more, than Css
- iOS学习笔记41-Swift(一)基础部分
- 【参考】spring整合activemq
- poj-2718-Smallest Difference
- python基础一
- 对三个超市销量、品牌数、均价聚类
- poj 3275 Ranking the Cows 传递闭包+邻接表优化
- ubuntu chrome