Reverse a String
题目:
翻转字符串
先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串。
你的结果必须得是一个字符串
这是一些对你有帮助的资源:
function reverseString(str) {
return str.split('').reverse().join('');
}
这里用到了一个字符串方法和两个数组方法,split()方法将一个String对象分割成字符串数组,通过将字符串分成子串,该方法返回一个数组。reverse() 方法颠倒数组中元素的位置。第一个元素会成为最后一个,最后一个会成为第一个。join() 方法将数组(或一个类数组对象)的所有元素连接到一个字符串中。
split()方法可以接受两个参数,第一个是分隔符,第二个参数可选,用于指定数组的大小,比如
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() 方法将数组的所有元素连接到一个字符串中。
var a = ['Wind', 'Rain', 'Fire'];
var b=a.join(" ");
console.log(b); // "Wind Rain Fire"
console.log(a); // ['Wind', 'Rain', 'Fire']
哈哈,今天第一次写博客,好激动,不足之处还望大神不吝赐教。
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 ...
- #254 Reverse a String
翻转字符串 先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串. 你的结果必须得是一个字符串 这是一些对你有帮助的资源: Global String Object ...
- 【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 ...
- 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 ...
随机推荐
- CIFAR和SVHN在各CNN论文中的结果
CIFAR和SVHN结果 加粗表示原论文中该网络的最优结果. 可以看出DenseNet-BC优于ResNeXt优于DenseNet优于WRN优于FractalNet优于ResNetv2优于ResNet ...
- WebMagic 爬虫框架
官方网站[http://webmagic.io/](http://webmagic.io/) >webmagic是一个开源的Java垂直爬虫框架,目标是简化爬虫的开发流程,让开发者专注于逻辑功能 ...
- Ubuntu16.04安装印象笔记
Nixnote 是一个 Evernote 开源客户端,原名 Nevernote.Evernote 是一个著名的笔记等个人资料整理和同步软件, 因为 Evernote 没有 Linux 下的官方版本,因 ...
- PreparedStatement和Statement区别详解
技术原理 该 PreparedStatement接口继承Statement,并与之在两方面有所不同: PreparedStatement 实例包含已编译的 SQL 语句.这就是使语句“准备好”.包含于 ...
- jquery试题
1.下面哪种说法是正确的? 您的回答:jQuery 是 JavaScript 库 2.jQuery 使用 CSS 选择器来选取元素? 您的回答:正确 3.jQuery 的简写是? 您的回答:$ 符号 ...
- 20135302魏静静Linux内核分析第二周学习总结
操作系统是如何工作的 1. 小结:计算机是怎样工作的 三个法宝 存储程序计算机.函数调用堆栈.中断机制 两把宝剑 中断上下文.进程上下文的切换 2. 堆栈 堆栈是C语言程序运行时必须的一个记录调用路径 ...
- 快用Visual Studio(五)- 语言特性
HTML 自带Emment $ SHIFT + OPTION + F:格式化代码 其他语言特性提示 CSS & LESS hover属性,提示样式对象标签 $ CMD + SHIFT + O: ...
- openwrt生成的镜像放在哪里
答:1.打包好之后是放在build_dir/target-$(cross-compile-toolchan-name)/linux-$(chip-series-name)_$(chip-arch)/t ...
- [BZOJ1722]Milk Team Select 产奶比赛
Description Farmer John's N (1 <= N <= 500) cows are trying to select the milking team for the ...
- 连续取模-function
2017-09-22 21:56:08 The shorter, the simpler. With this problem, you should be convinced of this tru ...