Leet Code OJ 344. Reverse String [Difficulty: Easy]
来源:互联网 发布:一台电脑装两个数据库 编辑:程序博客网 时间:2024/06/05 16:59
题目:
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
翻译:
写一个函数,使用字符串作为输入,返回它反转后的结果。
例如,输入”hello”,返回”olleh”。
分析:
转为字符数组后,将第一个字符和最后一个字符对调,第二个字符和倒数第二个对调,以此类推。
Java版代码(时间复杂度O(n),空间复杂度O(n)):
public class Solution { public String reverseString(String s) { char[] chars=s.toCharArray(); int len=chars.length; char temp; for(int i=0;i<len/2;i++){ temp=chars[i]; chars[i]=chars[len-1-i]; chars[len-1-i]=temp; } return new String(chars); }}
0 0
- Leet Code OJ 344. Reverse String [Difficulty: Easy]
- Leet Code OJ 7. Reverse Integer [Difficulty: Easy]
- Leet Code OJ 206. Reverse Linked List [Difficulty: Easy]
- Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]
- Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]
- Leet Code OJ 292. Nim Game [Difficulty: Easy]
- Leet Code OJ 242. Valid Anagram [Difficulty: Easy]
- Leet Code OJ 231. Power of Two [Difficulty: Easy]
- Leet Code OJ 258. Add Digits [Difficulty: Easy]
- Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
- Leet Code OJ 283. Move Zeroes [Difficulty: Easy]
- Leet Code OJ 217. Contains Duplicate [Difficulty: Easy]
- Leet Code OJ 169. Majority Element [Difficulty: Easy]
- Leet Code OJ 263. Ugly Number [Difficulty: Easy]
- Leet Code OJ 100. Same Tree [Difficulty: Easy]
- Leet Code OJ 70. Climbing Stairs [Difficulty: Easy]
- Leet Code OJ 202. Happy Number [Difficulty: Easy]
- Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]
- mysql忘记密码怎么办
- codesmith连接Mysql提示“找不到请求的 .Net Framework Data Provider。可能没有安装。"
- 文章标题
- 关于举办第三届全国Revit开发实战训练课程的通知
- 经验树 ( Experience Tree)
- Leet Code OJ 344. Reverse String [Difficulty: Easy]
- 竹枝词九首之七
- php 理解链式调用
- vim查找/替换字符串
- POJ 3616 Milking Time dp入门
- 使用IntelliJ IDEA 14和Maven创建java web项目
- oracle 数据库的约束
- 42、JavaScript的运动----多个物体同时运动
- CentOS7 下安装 Cacti0.8.8h 笔记