13. Roman to Integer
# Easy
Two pointers problem. One is current, the other one is next. Only two situations:
roman{cur} < roman{next}, such as 90 = 100 - 10, num{90} = 'XC'
roman{cur} >= roman{next}, eg1, 110 = 100 + 10, num{110} = 'CX', eg2, num{80} = 'LXXX'
其实不容易想到。
Solution:
if
roman{cur} < roman{next}
, two letters must be computed together,cur
andnext
pointers are moved 2 steps.if
roman{cur} >= roman{next}
, then only collect roman{cur}, cur and next pointers are moved 1 step.Don't forget to consider one last edge situation, if
cur
points to last element, add it tonum
directly, ifcur
points out of range, ignore it.
Last updated