【LeetCode】Reverse String 解题报告
来源:互联网 发布:知画生孩子摔倒的视频 编辑:程序博客网 时间:2024/05/19 16:19
Reverse String
[LeetCode]
https://leetcode.com/problems/reverse-string/
Total Accepted: 11014 Total Submissions: 18864 Difficulty: Easy
Question
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
Ways
方法一
字符串按位翻转:
public class Solution { public String reverseString(String s) { StringBuffer answer=new StringBuffer(""); int tail=s.length()-1; for(int i=tail;i>=0;i--){ answer.append(s.charAt(i)); } return answer.toString(); }}
AC:6ms
方法二
转换为字符串后,in-place翻转
public class Solution { public String reverseString(String s) { char[] chars=s.toCharArray(); for(int i=0;i<chars.length/2;i++){ char temp=chars[i]; chars[i]=chars[chars.length-1-i]; chars[chars.length-1-i]=temp; } return new String(chars); }}
AC:3ms
Date
2016/4/29 21:27:57
0 0
- 【LeetCode】Reverse String 解题报告
- [leetcode] 344. Reverse String 解题报告
- 【LeetCode】344. Reverse String 解题报告
- LeetCode解题报告 344. Reverse String [easy]
- 【LeetCode】Reverse String II 解题报告
- [Leetcode] 344. Reverse String 解题报告
- [Leetcode] 541. Reverse String II 解题报告
- leetcode解题报告 Reverse Words in a String
- LeetCode 解题报告 Reverse Words in a String
- [LeetCode OJ] Reverse Words in a String 解题报告
- 【LeetCode】Reverse Words in a String 解题报告
- [leetcode] 151. Reverse Words in a String 解题报告
- [leetcode] 186. Reverse Words in a String II 解题报告
- [leetcode] 345. Reverse Vowels of a String 解题报告
- 【LeetCode】Reverse Vowels of a String 解题报告
- Leetcode #344. Reverse String 逆转字符串 解题报告
- 【LeetCode】345. Reverse Vowels of a String 解题报告
- leetcode 345. Reverse Vowels of a String 解题报告
- 订单捕获-销售订单自定义
- 线性表的顺序存储结构(一)
- vtk实战(三十三)——写入vts数据
- HDOJ(HDU) 2083 简易版之最短距离(中位数)
- POJ 3087 Shuffle'm Up
- 【LeetCode】Reverse String 解题报告
- 多线程基础小结
- 基础卷_异常篇 第3集 对多异常的处理
- FOJ 1914 Funny Positive Sequence
- Atitit.预定义变量与变量预处理器
- 有趣的单机单节点现象
- java中的修饰符final和static
- Atitit.jpg png格式差别以及解决jpg图片不显示的问题
- Spring——AOP