Today let’s look at a relatively simple Leetcode problem, number 12, number conversion problem and let’s look at the problem first

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
Copy the code

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:\

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

That’s the title of the original. Let’s look at an official example: “Example1”

Input: 3
Output: "III"
Copy the code

Example2

Input: 4
Output: "IV"
Copy the code

Example3

Input: 9
Output: "IX"
Copy the code

The Roman numerals 4 and 9 are made up of IV and IX, equivalent to 5-1 and 10-1, all the way down to 40(XL),90(XC),400(CD), and 900(CM) by subtracting multiples of 5 and 10. When I first saw this problem, I was also wondering whether it was necessary to use all kinds of complex data structures, complex algorithms? Later I found some information, I found that this problem has a relatively easy to understand approach, that is, direct comparison let’s look at the code first

func intToRoman(num int) string {
 one := []string{""."I"."II"."III"."IV"."V"."VI"."VII"."VIII"."IX"} / / 1,2,3,4,5,6,7,8,9
 ten := []string{""."X"."XX"."XXX"."XL"."L"."LX"."LXX"."LXXX"."XC"} / / 10,20,30,40,50,60,70,80,90
 hundred := []string{""."C"."CC"."CCC"."CD"."D"."DC"."DCC"."DCCC"."CM"} / / 100200300400500600700800900
 thousand := []string{""."M"."MM"."MMM"}/ / 1000200, 0300
 if num < 10 {  return one[num]  }else if num >= 10 && num <= 99 {  t := num / 10  o := num % 10  return ten[t] + one[o]  }else if num >= 100 && num <= 999{  h := num / 100  t := num / 10 % 10  o := num % 10  return hundred[h] + ten[t] + one[o]  }else {  th := num / 1000  h := num / 100 % 10  t := num / 10 % 10  o := num % 10  return thousand[th] + hundred[h] + ten[t] + one[o]  } } Copy the code

The code is pretty simple, there’s no complicated for loops, there’s nothing too complicated about it, it’s pretty simple to do, but let’s go through the idea a little bit and start with mapping strings into a table

I’ve defined a list of ten, hundreds, thousands of units here

There are two important points here.

The second thing about mod numbers, let’s figure out if a number is greater than 1,000 how do we take the thousands place of that number?

You know how to do it? Simple mathnums/1000I get the thousands place

nums / 100 % 10Get a one hundred – bit

nums / 10 % 10For ten

nums % 10Get the bits

So with that knowledge, you don’t have to make four judgments like I did, but there should be a faster way to return a string, so I’ll leave it to you.

And there is also room to save a certain string, also given to you to optimize \