CodingTour
ARTS #1

Algorithm

本周选择的算法题是:Roman to Integer

规则如下:

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Solution

我实现的方案超过了 99.41% 的提交数。

根据规则,左边的数可能比右边小,如CMXC,同时这类组合长度最多为两位数,所以我用传统的方式从左往右遍历,先记录第一位,再根据第二位来执行不同的逻辑:

class Solution:
    
    roman = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 }
    
    def romanToInt(self, s: str) -> int:
        if len(s) == 0:
            return 0
        
        s = s.upper()
        
        total = 0
        reserve = 0
        for char in s:
            i = Solution.roman[char]
                
            if reserve == 0:
                reserve = i
                continue
            
            if i > reserve:
                total = i - reserve + total
                reserve = 0
            elif i == reserve:
                total = i + reserve + total
                reserve = 0
            else:
                total = total + reserve
                reserve = i
            
        return total + reserve

代码看起来很冗余,不够简洁,下面这个解决方案就很好:

class Solution(object):
	def romanToInt(self, s):
		dic = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
		prev = None
		count = 0
		for i in range(len(s)-1,-1,-1):
			curr = s[i]
			if prev is not None and dic[prev] > dic[curr]:
				count-=dic[curr]
			else:
				count+=dic[curr]
			prev = curr
		
		return count

这个实现是从右往左遍历,逻辑一下子就简单了。

Review

7 Practical Tips for Cheating at Design
这篇文章分享了 7 个瞬间让 Web 页面高逼格的技巧,成本低、见效快,适用于不想花太多时间,但是又对设计有一定要求的人。

我将其中的一些技巧用在了这个 GitHub Pages 站点上。

Tip

本周学习到的一些内容:

  • 重新泛读了一遍 Autorelease Pool 的源码,算是温习
  • 学习到了一些财务、投资知识,放下心中焦虑

Share

本周分享字典的实现方式:

什么是 HashMap
描述了主要的设计思路

HashMap 的扩容机制—resize()
对上篇的补充

为啥要用位运算代替取模呢
一个细节点,位运算需要的 CPU 时钟周期远远小于取模所需要的 CPU 时钟周期,尽管各个 CPU 架构不同,但是这个结论应该没错