Leet Code OJ 344. Reverse String [Difficulty: Easy]
题目:
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);}
}
Leet Code OJ 344. Reverse String [Difficulty: Easy]相关推荐
- Leet Code OJ 7. Reverse Integer [Difficulty: Easy]
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...
- Leet Code OJ 28. Implement strStr() [Difficulty: Easy]
题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...
- Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
题目: Given a string containing just the characters , determine if the input string is valid. The brac ...
- Leet Code OJ 112. Path Sum [Difficulty: Easy]
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- Leet Code OJ 242. Valid Anagram [Difficulty: Easy]
题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...
- Leet Code OJ 1. Two Sum [Difficulty: Easy]
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- Leet Code OJ 223. Rectangle Area [Difficulty: Easy]
题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...
- Leet Code OJ 189. Rotate Array [Difficulty: Easy]
题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...
最新文章
- 伦敦大学学院、UC伯克利联手,撰文综述深度强化学习泛化研究
- 2009年9月三级网络技术51CTO版考前压轴试题
- 正确地使用投影仪和移动硬盘
- 实现简单render函数
- VS系列之【 产品密钥 – 所有版本】
- ARM指令集发展概述
- vue——回到顶部监听滚动事件
- 判了!国内「最牛删库跑路事件」程序员被判6年,公司损失近亿
- 发生内部错误。请与 microsoft 支持部门联系。_设计变更、工程签证、确认单、工程洽商、联系单、会签的是干啥的?有啥区别?...
- 班级网站-前端网页技术精美完整源码HTML+CSS+JS
- font awesome矢量字体使用
- 锐捷交换机(S2924/2928G)光口改电口,或电口改光口的准确方法
- 【Linux】从冯诺依曼体系到初识Linux下的进程
- 目前5G SoC 芯片技术成熟吗
- [随手记系列]SQLite数据库.db | .db-wal | .db-shw到底是个啥,为什么我导出.db里面没数据呢?
- 计算机项目 rolling ball
- 最全最走心:模电-模拟电子技术 复试/面试知识点大总结 (已更完)
- 【LeNet、AlexNet、VGG】
- sham-link和路由选路
- 大数据未来应用的五大发展方向