【LeetCode】Reverse String 解题报告
2016-04-29 21:29
435 查看
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相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解