LeetCode Reverse String
原题链接在这里:https://leetcode.com/problems/reverse-string/
题目:
Write a function that reverses a string. The input string is given as an array of characters char[]
.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
Example 1:
Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
题解:
Reverse string是常见题, string在Java中是primitive类型, 一旦生成不可改变.
But we have the array of char here. Then use two pointers. Remember to move pointer in while loop.
Time Complexity: O(s.length).
Space: O(1).
AC Java:
class Solution {
public void reverseString(char[] s) {
int l = 0;
int r = s.length - 1; while(l < r){
char temp = s[l];
s[l] = s[r];
s[r] = temp; l++;
r--;
}
}
}
跟上Reverse String II, Reverse Vowels of a String.
LeetCode Reverse String的更多相关文章
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- [LeetCode] Reverse String II 翻转字符串之二
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- LeetCode Reverse String II
原题链接在这里:https://leetcode.com/problems/reverse-string-ii/#/description 题目: Given a string and an inte ...
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers
344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...
- LeetCode Reverse Words in a String III
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...
随机推荐
- [MongoDB]安装MongoDB
汇总: 1. [MongoDB]安装MongoDB2. [MongoDB]Mongo基本使用:3. [MongoDB]MongoDB的优缺点及与关系型数据库的比较4. [MongoDB]MongoDB ...
- partial class的使用范围
Partial Class,部分类 或者分布类.顾名思义,就是将一个类分成多个部分.比如说:一个类中有3个方法,在VS 2005将该类中3个方法分别存放在3个不同的.cs文件中. 这样做的好处: 1. ...
- linq in not in
class A { public int B { get; set; } public string C { get; set; } } class Program { static void Mai ...
- java调用mysql服务做备份与恢复
首先添加mysql的bin到环境变量,这样可以简写部分命令,并且做到不依赖系统mysql的具体安装路径. 重启计算机可以让添加的环境变量在java代码中调用时生效.(cmd中生效但java中调用没有生 ...
- C# 默认以管理员权限运行程序
/** * 当前用户是管理员的时候,直接启动应用程序 * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 */ //获得当前登录的Windows用户标示 //URL:http://w ...
- ASP.NET Cache缓存的用法
本文导读:在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序级别的 ...
- Unity3d uGUI适配
Cavas: 1.Render Model设置为:Screen Space- Camera.如果想在UI前面加特效我可以在创建一个摄像机(UIForward)深度大于这个UICamera就行了. 2. ...
- hdu4135 容斥定理
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- ZooKeeper学习总结 第二篇:ZooKeeper深入探讨(转载)
其实zookeeper系列的学习总结很早就写完了,这段时间在准备找工作的事情,就一直没有更新了.下边给大家送上,文中如有不恰当的地方,欢迎给予指证,不胜感谢!. 1. 数据模型 1.1. 只适合存储小 ...
- java.lang.NoClassDefFoundError:
异常信息:十一月 10, 2016 5:20:15 下午 org.apache.catalina.core.StandardContext loadOnStartup严重: Servlet /mgr ...