翻转字符串

先把字符串转化成数组,再借助数组的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的更多相关文章

  1. reverse the string word by word

    题目:Given an input string, reverse the string word by word. For example,Given s = "the sky is bl ...

  2. 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 ...

  3. 【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 ...

  4. 【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 ...

  5. Reverse a String

    题目: 翻转字符串 先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串. 你的结果必须得是一个字符串 这是一些对你有帮助的资源: Global String Ob ...

  6. Leetcode 题目整理-2 Reverse Integer && String to Integer

    今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...

  7. FCC JS基础算法题(0):Reverse a String(翻转字符串)

    题目描述: 先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串.你的结果必须得是一个字符串. 算法: function reverseString(str) { ...

  8. [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 ...

  9. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

随机推荐

  1. java中null是什么,以及使用中要注意的事项

    1.null既不是对象也不是一种类型,它仅是一种特殊的值,你可以将其赋予任何引用类型,你也可以将null转化成任何类型,例如: Integer i=null; Float f=null; String ...

  2. Shell 运算相关

    一.${str} 二.变量替换 三.3种计算字符串长度的效率比较 四.(())用法 五.expr 六.bc 4种连续整数求和效率 七.条件测试 八.字符串测试

  3. Linux debug

    proc文件系统中可以查看一些正在运行的变量如device-tree sh-3.2# cat /proc/device-tree/ #address-cells fixedregulator@9/ # ...

  4. ROC AUC

    1.什么是性能度量? 我们都知道机器学习要建模,但是对于模型性能的好坏(即模型的泛化能力),我们并不知道是怎样的,很可能这个模型就是一个差的模型,泛化能力弱,对测试集不能很好的预测或分类.那么如何知道 ...

  5. centos7+mariadb+防火墙,允许远程

    centos7 已安装mariadb,想要允许数据库远程==数据库权限允许+系统允许 mariadb:允许数据库用户在所有ip使用某个用户远程 GRANT ALL PRIVILEGES ON *(数据 ...

  6. linux(centos) tomcat设置开机启动

    亲测有效 环境: centos7 apache-tomcat-8.5.37 设置步骤: 1.修改/etc/rc.d/rc.local vi /etc/rc.d/rc.local 2.添加下面两行脚本, ...

  7. python 数据可视化 -- 真实数据的噪声平滑处理

    平滑数据噪声的一个简单朴素的做法是,对窗口(样本)求平均,然后仅仅绘制出给定窗口的平均值,而不是所有的数据点. import matplotlib.pyplot as plt import numpy ...

  8. 获取Vue的实例方法

    我们知道在new Vue({...})后,如果没有赋值给一个变量存储,我们很难拿到这个实例,Vue官方也没有提供Vue.getInstance方法,那我们就自己扩展个吧 Code: Vue.getIn ...

  9. java 多线程的唤醒

    package TestThread.ThreadSynchronized.TestInterruptedException; public class InterruptDemo { public ...

  10. 使用signalr实现网页和微信公众号实时聊天(上)

    最近项目中需要实现客户在公众号中和客服(客服使用后台网站系统)进行实时聊天的功能.折腾了一段时间,实现了这个功能.现在将过程记录下,以便有相同需求的同行可以参考,也是自己做个总结.这篇是上,用手机编辑 ...