#254 Reverse a String
翻转字符串
先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串。
你的结果必须得是一个字符串
这是一些对你有帮助的资源:
|
1
2
3
|
function reverseString(str) { return str.split('').reverse().join('');} |
这里用到了一个字符串方法和两个数组方法,split()方法将一个String对象分割成字符串数组,通过将字符串分成子串,该方法返回一个数组。reverse() 方法颠倒数组中元素的位置。第一个元素会成为最后一个,最后一个会成为第一个。join() 方法将数组(或一个类数组对象)的所有元素连接到一个字符串中。
split()方法可以接受两个参数,第一个是分隔符,第二个参数可选,用于指定数组的大小,比如
|
1
2
3
4
|
var myString = "Hello World. How are you doing?";var splits = myString.split(" ", 3);console.log(splits); // ["Hello", "World.", "How"]console.log(myString); //"Hello World. How are you doing?" |
reverse() 方法颠倒数组中元素的位置。第一个元素会成为最后一个,最后一个会成为第一个。该方法没有参数。
join() 方法将数组的所有元素连接到一个字符串中。
|
1
2
3
4
|
var a = ['Wind', 'Rain', 'Fire'];var b=a.join(" ");console.log(b); // "Wind Rain Fire"console.log(a); // ['Wind', 'Rain', 'Fire'] |
#254 Reverse a String的更多相关文章
- reverse the string word by word
题目:Given an input string, reverse the string word by word. For example,Given s = "the sky is bl ...
- LeetCode 5:Given an input string, reverse the string word by word.
problem: Given an input string, reverse the string word by word. For example: Given s = "the sk ...
- 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- Reverse a String
题目: 翻转字符串 先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串. 你的结果必须得是一个字符串 这是一些对你有帮助的资源: Global String Ob ...
- Leetcode 题目整理-2 Reverse Integer && String to Integer
今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...
- FCC JS基础算法题(0):Reverse a String(翻转字符串)
题目描述: 先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串.你的结果必须得是一个字符串. 算法: function reverseString(str) { ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
随机推荐
- 洛谷P4051 [JSOI2007]字符加密 后缀数组
题目链接:https://www.luogu.org/problemnew/show/P4051 思路:我们联想求后缀数组sa的过程,发现我们在求y数组的时候(第二关键字,下标为第二关键字的排位,值为 ...
- Android 菊花加载工具类
先看看实现效果图 1.首先自定义一个类继承系统ProgressDialog /** * Created by hanbao0928 on 2018/11/1. */ public class Dial ...
- WebService关于Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart)问题解决
错误原因:需要mail.jar和activation.jar. Solution:Web Services Required Jars Download Instructions http://www ...
- java实现单例模式
1.饿汉模式 public class Singleton{ private static Singleton instance = new Singleton(); private Singleto ...
- 有关Lambda的一些思考
问题: What do lambda expressions do? Can we write all functions as lambda expressions? In what cases a ...
- 控制请求重复提交的方法总结(Token)
重复提交的定义: 重复提交指的是同一个请求(请求地址和请求参数都相同)在很短的时间内多次提交至服务器,从而对服务器造成不必要的资源浪费,甚至在代码不健壮的情况还会导致程序出错. 重复提交的原因或触发事 ...
- Python中的计时器对象
计时器对象用于特定时间运行的操作.往往被安排到特定的单独的线程上运行, 但是计时器初始化的时间间隔可能不是解释器实际执行操作时的实际时刻, 因为线程调度程序负责实际调度与计时器对象相对应的线程. Ti ...
- SQLServer 中有五种约束, Primary Key 约束、 Foreign Key 约束、 Unique 约束、 Default 约束和 Check 约束
一直在关注软件设计方面,数据库方面就忽略了很多,最近在设计数据库时遇到了一些小麻烦,主要是数据库中约束和性能调优方面的应用,以前在学习 Sql Server 2000,还有后来的 Sql Server ...
- IDEA 上 Tigase8.0 源代码编译运行
Tigase交流群 :310790965 一些小伙伴们经常问Tigase8 check下来的源代码怎么跑起来,因为我不能及时一 一回答,所以还是写个博文来演示一下吧,一般针对新手而言,老手的话,就跳过 ...
- struts1.x 核心控制器 和 用户自定义控制器扩展类;
ServletAction继承于HttpServlet,是struts1.x中和核心控制器. 配置于web.xml文件中,指定config属性,该config属性用于指定formBean和action ...