ARTS
Sun, 25 Nov 2018 16:27:24 +0000
ARTS
- A (Algotithm) 至少做一个leetcode的算法题
- R (Review) 阅读并点评一篇英文的技术文章
- T (Tip) 学习一个技术技巧
- S (Share) 分享一篇有观点和思考的技术文章
每周一次,坚持一年
Algorithm
Description ZigZag Conversion
Solution
/**
* https://leetcode.com/problems/zigzag-conversion/
* @param {string} s
* @param {number} numRows
* @return {string}
*/
var convert = function(s, numRows) {
if (numRows == 1) return s;
// let rows = new Array(numRows);
var rows = [];
for (var i = 0; i < numRows; i++) {
rows[i] = [];
}
var curRow = 0;
var goingDown = false;
for (var i = 0; i < s.length; i++) {
// if (!rows[curRow]) rows[curRow] = [];
rows[curRow].push(s[i]);
if (curRow === 0 || curRow === numRows - 1) goingDown = !goingDown;
curRow += goingDown ? 1 : -1;
}
var result = '';
for (var i = 0; i < rows.length; i++) {
for (var j = 0; j < rows[i].length; j++) {
result += rows[i][j];
}
// result += rows[i].join(''); // 原生的 for 循环更快。
// result = result.concat(rows[i]); // concat 更慢。。。
}
return result;
};
console.log(convert('PAYPALISHIRING', 3), 'PAHNAPLSIIGYIR', convert('PAYPALISHIRING', 3) === 'PAHNAPLSIIGYIR');
console.log(convert('PAYPALISHIRING', 4), 'PINALSIGYAHRPI', convert('PAYPALISHIRING', 4) === 'PINALSIGYAHRPI');
console.log(convert('Apalindromeisaword,phrase,number,orothersequenceofunitsthatcanbereadthesamewayineitherdirection,withgeneralallowancesforadjustmentstopunctuationandworddividers.', 2)
===
"Aaidoeswr,haenme,rtesqecouishtabrateaeaietedrcinwtgnrlloacsoajsmnsoucutoadodiiesplnrmiaodprs,ubroohreunefnttacneedhsmwynihrieto,iheeaalwnefrdutettpntainnwrdvdr."
)
// convert('PAYPALISHIRING', 3);
Review
Exploring EcmaScript Decorators https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841
探索 JS 装饰器
装饰器就是接收一个函数并返回具有附加功能的函数。装饰器为调用高阶函数提供了一种非常简单的语法。
Tip
bee’s knees
出類拔萃的人或事物
https://www.businessweekly.com.tw/article.aspx?id=18435&type=Blog
Share
未完待续…
blog comments powered by Disqus