翻转字符串

先把字符串转化成数组,再借助数组的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. python note 15 正则表达式

    # 正则表达式 只和字符串打交道 # 正则表达式的规则# 规则 字符串 从字符串中找到符合规则的内容 # 字符组 : [] 写在中括号中的内容,都出现在下面的某一个字符的位置上都是符合规则的 # [0 ...

  2. php tp3.2生成二维码

    public function qrcode() { // 二维码链接 $href = 'http://' . $_SERVER['HTTP_HOST'] . '/?g=diapp&m=reg ...

  3. centos7下编译安装nginx-1.16.0

    一.下载nginx源码 http://nginx.org/en/download.html 如:nginx-1.16.0.tar.gz 二.创建用户和组,并解压 groupadd www userad ...

  4. Redis:MySQL算老几?

    原创: 码农翻身刘欣 前言:上一篇<MySQL:缓存算什么东西?>里挖了一个坑,也有很多人说没看过瘾,今天接着写,把坑填上,不过得把视角换一下,让Redis上台发言. 我知道MySQL看我 ...

  5. sonar之阿里巴巴java规则(p3c)

    今天为了打包生成sonar-p3c-pdm插件,折腾了半天.sonar版本v6.7.6,p3c源码地址https://gitee.com/jasonlong10/sonar-p3c-pmd-plugi ...

  6. docker-2 tomcat

    启动容器命令 docker run -d -p 8080:8080 -v /root/tomcat/webapps:/usr/local/tomcat/webapps -v /root/tomcat/ ...

  7. CSRedisCore 在net core中的使用

    背景:与net core配套的StackExchange.Redis客户端总是间歇性的发生timeout异常. 由complexer单例对象创建的IDatabase对象,在产生Timeout异常后会导 ...

  8. css -html-文档流

    首先先考虑一下什么是普通流?普通流就是正常的文档流,在HTML里面的写法就是从上到下,从左到右的排版布局. 例: <div id="01"></div>&l ...

  9. FTP做作业用到的知识点:

    FTP做作业用到的知识点: 一: os.path 模块下常用的用法 os.path.abspath(file) #返回的是.py文件的绝对路径(完整路径) os.path.dirname(file) ...

  10. js设置高度和宽度相等

    <!doctype html><html><head><meta charset="utf-8"><link rel=&quo ...