每週學算法/讀英文/知識點心得分享 2.18 - 2.22

 每週一個 Algorithm,Review 一篇英文文章,總結一個工作中的技術 Tip,以及 Share 一個傳遞價值觀的東西!

 

Algorithm: 學習算法

題目:Integer to Roman

題目描述:

Roman numerals are represented by seven different symbols: IVXLCD 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 an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: 3
Output: "III"

Example 2:

Input: 4
Output: "IV"

Example 3:

Input: 9
Output: "IX"

Example 4:

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

Example 5:

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

解題過程:

個人認爲是思路比較清晰的一道題,就是轉換一種進制來表示數。

除開通用表示外,有六個數是用除法表示的。

編寫代碼時注意使用一個進制對照表,這樣可以把進制轉化的過程抽象成for循環,而不是許多個if判斷。

解法:https://leetcode.com/submissions/detail/209466062/

 

Review: 學習英文

題目:Inversion of Control Containers and the Dependency Injection pattern

內容概述:馬丁大叔剖析IoC容器背後的原理就是DI pattern,然後使用例子闡述了constructor injection, setter injection and interface injection,接着講了Service Locator pattern,它和DI模式的目的都是爲了解耦服務提供者和客戶端,通過字符串映射服務名稱。

 

Tips: 知識點

 無

Share: 價值觀

知識金字塔模型告訴我們,把一個知識點分享出來會理解更深刻。今天下午會分享Effective Java裏關於重寫hashCode的必要性這一點。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章