Tue, 01 Jan 2019 15:47:00 +0000
  • A (Algotithm) 至少做一个leetcode的算法题
  • R (Review) 阅读并点评一篇英文的技术文章
  • T (Tip) 学习一个技术技巧
  • S (Share) 分享一篇有观点和思考的技术文章

每周一次,坚持一年

Algorithm

Description Roman to Integer

Solution

/**
 * https://leetcode.com/problems/roman-to-integer/
 * @param {string} s
 * @return {number}
 */
var romanToInt = function(s) {
    var arr = s;
    var result = 0;
    // arr = [ 'I1', 'I2', 'I3' ];
    // console.log('arr', arr);

    // arr.reduce(function(x, y) {
    //     // console.log('x, y', x, y)
    //     if (romanValue[x] >= romanValue[y]) {
    //         result += (romanValue[x]);
    //     } else {
    //         result -= (romanValue[x]);
    //     }
    //     return y;
    // });

    // for 循环比 reduce 应该要快一些。
    for(var arrIndex = 1; arrIndex < arr.length; arrIndex++) {
        // console.log('arr[arrIndex - 1]', arr[arrIndex - 1], arr[arrIndex]);
        var last = romanValue[arr[arrIndex - 1]];
        // console.log('...last', last, romanValue[arr[arrIndex]]);
        if (last >= romanValue[arr[arrIndex]]) {
            result += last;
        } else {
            result -= last;
        }
    }

    result += romanValue[arr[arr.length - 1]];
    return result;
};

var romanValue = {
    'I': 1,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500,
    'M': 1000,
};

// console.log('romanToInt(\'IV\')', romanToInt('IV'), 4);


console.log('romanToInt(\'I\')', romanToInt('I'), 1);
console.log('romanToInt(\'II\')', romanToInt('II'), 2);
console.log('romanToInt(\'III\')', romanToInt('III'), 3);
console.log('romanToInt(\'IV\')', romanToInt('IV'), 4);
console.log('romanToInt(\'V\')', romanToInt('V'), 5);
console.log('romanToInt(\'VI\')', romanToInt('VI'), 6);
console.log('romanToInt(\'VII\')', romanToInt('VII'), 7);
console.log('romanToInt(\'VIII\')', romanToInt('VIII'), 8);
console.log('romanToInt(\'IX\')', romanToInt('IX'), 9);
console.log('romanToInt(\'XIX\')', romanToInt('XIX'), 19);
console.log('romanToInt(\'XL\')', romanToInt('XL'), 40);
console.log('romanToInt(\'CXLIV\')', romanToInt('CXLIV'), 144);
console.log('romanToInt(\'CD\')', romanToInt('CD'), 400);

console.log('romanToInt(\'LVIII\')', romanToInt('LVIII'), 58);
console.log('romanToInt(\'MCMXCIV\')', romanToInt('MCMXCIV'), 1994);

console.log('romanToInt(\'XC\')', romanToInt('XC'), 90);
console.log('romanToInt(\'XCI\')', romanToInt('XCI'), 91);
console.log('romanToInt(\'XCIX\')', romanToInt('XCIX'), 99);
console.log('romanToInt(\'CM\')', romanToInt('CM'), 900);
console.log('romanToInt(\'CMXCIX\')', romanToInt('CMXCIX'), 999);
console.log('romanToInt(\'MCMXCIX\')', romanToInt('MCMXCIX'), 1999);
console.log('romanToInt(\'MM\')', romanToInt('MM'), 2000);

Review

Things I Don’t Know as of 2018 https://overreacted.io/things-i-dont-know-as-of-2018/?from=timeline&isappinstalled=0

该篇文章是作者阐述自己至 2018 年还未了解的相关技术。

作者表示很多你崇拜的大神也会有你会但是大神不会的技术。

作者承认自己也会有知识缺陷但是也有宝贵的需要多年才能达到的知识。

Tip

99%的程序都没有考虑的网络异常 https://mp.weixin.qq.com/s/S2Eu9coPeikkfIbc3KeusA 这篇文章我理解的就是处理一些平时常见但前端不怎么处理掉错误。用 try catch 解决。

  1. 返回接口数据报错。
  2. 服务不稳定导致的接口报错。
  3. 网络不稳定导致的接口报错。

Share



blog comments powered by Disqus