Comments (6)
用for循环是不是要比while好点,逻辑上也更清晰一点
from fe-learning.
利用栈
String.prototype.str_reverse = function() {
let stack = [],
_str = '',
str = this;
for (let i = str.length - 1; i >= 0; i--) {
let s = str[i]
if (s === '.') {
while (stack.length) {
_str += stack.pop();
}
_str += s
} else {
stack.push(s);
}
}
while (stack.length) {
_str += stack.pop();
}
return _str
}
from fe-learning.
利用栈
String.prototype.str_reverse = function() { let stack = [], _str = '', str = this; for (let i = str.length - 1; i >= 0; i--) { let s = str[i] if (s === '.') { while (stack.length) { _str += stack.pop(); } _str += s } else { stack.push(s); } } while (stack.length) { _str += stack.pop(); } return _str }
赞一个,不过在遇到 .
之前是不是可以不用入栈
for (let i = 0; i < str.length; i++) {
let s = str[i]
if (s === '.') {
stack.push(_str);
_str = ''
} else {
_str += s
}
}
while (stack.length) {
_str += '.' + stack.pop();
}
from fe-learning.
利用栈
String.prototype.str_reverse = function() { let stack = [], _str = '', str = this; for (let i = str.length - 1; i >= 0; i--) { let s = str[i] if (s === '.') { while (stack.length) { _str += stack.pop(); } _str += s } else { stack.push(s); } } while (stack.length) { _str += stack.pop(); } return _str }赞一个,不过在遇到
.
之前是不是可以不用入栈for (let i = 0; i < str.length; i++) { let s = str[i] if (s === '.') { stack.push(_str); _str = '' } else { _str += s } } while (stack.length) { _str += '.' + stack.pop(); }
good,第一版的split函数 + 栈 😂
from fe-learning.
头条的面试是不能开辟额外空间
from fe-learning.
我记得字符串是不能被更改的?所以怎么样都会有额外空间吧,感觉这样写更简洁一点
function reverse(str) {
let res = ''
for (const word of str) {
res = word + res
}
return res
}
from fe-learning.
Related Issues (20)
- 剑指offer —— 重建二叉树 HOT 1
- 关于async和await的“同步” HOT 1
- 实现一个深克隆函数
- 将JS对象表示的DOM结构渲染为真实DOM树
- 将list数据转换成树形结构
- 手写eventbus
- 实现Lodash函数系列(一):_.get HOT 2
- 京东面经 HOT 1
- 实现Promise.all及finally
- CSS绘制常见的图形
- CSS实现一个响应式布局
- 二叉树的路径
- 判断二叉树是否相同/对称/子树
- 树形数据结构扁平化 HOT 2
- 字符串相加和相乘
- 将数字转换为人民币单位格式 HOT 1
- 微信面试题 LazyMan
- flex相关属性的计算方法
- 实现一个repeat函数 HOT 4
- 记一次字节前端笔试
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fe-learning.