Sun, 11 Nov 2018 07:04:21 +0000

ARTS

  • A (Algotithm) 至少做一个leetcode的算法题
  • R (Review) 阅读并点评一篇英文的技术文章
  • T (Tip) 学习一个技术技巧
  • S (Share) 分享一篇有观点和思考的技术文章

每周一次,坚持一年

Algorithm

Description https://leetcode.com/problems/string-to-integer-atoi/

Implement atoi which converts a string to an integer.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Note:

Only the space character ' ' is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31,  2^31 − 1]. If the numerical value is out of the range of representable values, INT_MAX (2^31 − 1) or INT_MIN (−2^31) is returned.
Example 1:

Input: "42"
Output: 42
Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
             Then take as many numerical digits as possible, which gets 42.
Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical
             digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
             Thefore INT_MIN (−2^31) is returned.

Solution

/**
 * @param {string} str
 * @return {number}
 * https://leetcode.com/problems/string-to-integer-atoi/
 */
var myAtoi = function(str) {
    var result = parseInt(str, 10);
    if (isNaN(result)) {
        return 0;
    }
    if (result >= Math.pow(2, 31)) {
        return Math.pow(2, 31) - 1;
    } else if (result < -Math.pow(2, 31)) {
        return -Math.pow(2, 31);
    }
    return result;
};

console.log(myAtoi('42'), 42);
console.log(myAtoi('+-42'), 0);
console.log(myAtoi('     -42'), -42);
console.log(myAtoi('4193 with words'), 4193);
console.log(myAtoi('words and 987'), 0);
console.log(myAtoi('-91283472332'), -2147483648);

/**
 * https://segmentfault.com/a/1190000010571914
 * JavaScript 实现 parseInt()
 */

Review

Comparing Flow with TypeScript https://medium.com/the-web-tub/comparing-flow-with-typescript-6a8ff7fd4cbb

这篇文章指在比较 Flow 与 Typescript 的异同。

Flow 和 Typescript 皆是使 JavaScript 可以以静态类型的方式去书写,即可使用静态类型语音的优势。但是 Flow 比 Typescript 更简单,开箱即用、对库/框架/编译器更好的兼容性、它有能力保持 JS 的纯粹。只在项目开头添加 // @flow 即可。

Tip

JS: Array.reverse() vs. for and while loops https://jsperf.com/js-array-reverse-vs-while-loop/66

我们可以从这篇文章获知 JS 的数组颠倒使用 for 循环的 swap half、XOR swap half 效率最高。

Share

css 的 white-space

换行符 空格和 Tab 文本超出容器宽度 -
nomal 忽略 折叠 换行
nowrap 忽略 折叠 不换行
pre 换行 保持原样 不换行
pre-wrap 换行 保持原样 换行
pre-line 换行 折叠 换行

相关文档:



blog comments powered by Disqus