Post

[C++ Solution] Roman to Integer and Integer to Roman & Structure SpringBoot

Roman to Integer

1. Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
    int romanToInt(string s) {
        std::unordered_map<char, int> roman_to_int = {
            {'I', 1},
            {'V', 5},
            {'X', 10},
            {'L', 50},
            {'C', 100},
            {'D', 500},
            {'M', 1000}
        };
        int total = 0;
        int temp = 0;

        for (auto item = s.rbegin(); item != s.rend(); ++item) {
            int value = roman_to_int[*item];

            if (value < temp) {
                total -= value;
            } else {
                total += value;
            }
            temp = value;
        }
        return total;
    }
};

Integer To Roman

1. Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    string intToRoman(int num) {
        std::vector<std::pair<int, std::string>> romanSymbols = {
            {1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"},
            {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"},
            {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}
        };

        std::string result;
        for (const auto& [value, symbol] : romanSymbols) {
            while (num >= value) {
                result += symbol;
                num -= value;
            }
        }
        return result;
    }
};

Reviews

1. Vector

  • Example : std::vector ...

  • Best suited when maintaining order and sequentially processing data is important

1
In the case of converting numbers to Roman numerals, you need to handle the values from the largest to the smallest, so a vector is ideal

2. Map

  • Example : std::map , std::unordered_map

  • Best suited for scenarios where you need fast lookups by key, and the order of elements is not important

    However, for the Roman numeral conversion where order is critical, a vector is more appropriate

Spring Boot

Github : Potato

1. Building a Spring Boot Application

  • Model -> Repository -> Service -> Controller

A. Model

  • maps to a database table

  • It defines the data fields, types, and relationships between different data entities

  • Annotated with JPA (@Entity, @Id, @Column, etc)

B. Repository

  • It is an interface that handles the data access logic. It is a layer that directly interests with the database, allowing CRUD on the Model

  • By extending Spring DAta JPA’s JpaRepository interface, you get a wide range of data access functionalities with minimal code

C. Service

  • It acts as an intermediary between the Controller and the Repository, handling tasks like data processing, validation, and calling multiple repositories if needed

D. Controller

  • The Controller is responsible for handling HTTP requests from clients. It acts as the entry point to the application, receiving input, processing it (often by delegating to the Service layer), and returning a response

  • Typically using annotations @GetMapping, @PostMapping, etc.

    It ensures that the application can respond to web requests and interact with the client

This post is licensed under CC BY 4.0 by the author.