翻转字符串

先把字符串转化成数组,再借助数组的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. checkbox、radio控件和文字不对齐

    一般使用html控件的时候  单选按钮和复选框的控件和文字不对齐 给input控件加上   style="vertical-align: middle; margin-top: -2px; ...

  2. 移动端rem适配

    (function(_D) { var _self = {}; _self.resizeEvt = 'orientationchange' in window ? 'orientationchange ...

  3. RFID概述

    自动识别技术的本质在于利用被识别物理对象的一些具有辨识度的特征来对物理对象进行区分和识别.因此,这些具有辨识度的特征可以是物理对象自带的特征,如指纹,人脸,语言,视网膜,心跳等,也可以是通过第三方赋予 ...

  4. Python学习笔记6函数和代码复用

    1.函数 (1)定义: (2)函数调用 (3)函数的参数传递 参数传递的两种方式:函数调用时,参数可以按照位置或名称的方式传递 (4)函数的返回值 (5)局部变量和全局变量 (6)lambda函数 2 ...

  5. 生成二维码、条形码、带logo的二维码

    Nuget安装ZXing.Net,帮助类: using System; using System.Collections.Generic; using System.Drawing; using Sy ...

  6. echarts 图表重新加载,原来的数据依然存在图表上

    问题 在做一个全国地图上一些饼图,并且向省一级的地图钻取的时候,原来的饼图依然显示 原因 echars所有添加的图表都在一个series属性集合中,并且同一个echars对象默认是合并之前的数据的,所 ...

  7. ionic3自定义android原生插件

    一.创建一个android项目,编写插件功能,并测试ok,这里以一个简单的调用原生Toast.makeText为例. 1.新建android项目 2.编写插件类 package com.plugin. ...

  8. dskinlite(uieasy mfc界面库)使用记录1: schema验证xml

    市场上的MFC第三方库很多,最终选定dskinlite企业版,成熟度比较高,当然价格也略贵. 在2017年仍然使用MFC是有些另类,但特定场景很适用,也适合不愿转型的老程序员. 目前处于学习阶段,欢迎 ...

  9. iOS.redefinition-of-struct-x

    Error: Redefinition of struct x Reference

  10. 阿里oss图片上传

    <script type="text/javascript" src="../../static/js/manage/oss_uploader.js"&g ...