Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions clojure/12.clj

This file was deleted.

12 changes: 12 additions & 0 deletions solutions/0001-0100/0012-integer-to-roman/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 12. Integer to Roman

[LeetCode problem 12](https://leetcode.com/problems/integer-to-roman/)

- **Difficulty**: Medium
- **Tags**: hash-table, math, string, greedy

## Description

Given an integer in the range `[1, 3999]`, convert it to a Roman numeral,
handling the six subtractive pairs (`IV`, `IX`, `XL`, `XC`, `CD`, `CM`) that
Roman numerals use in place of four repeated symbols in a row.
37 changes: 37 additions & 0 deletions solutions/0001-0100/0012-integer-to-roman/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "solution.hpp"

#include <array>
#include <utility>

namespace leetcode::p0012 {

std::string solve(int num) {
static constexpr std::array<std::pair<int, const char*>, 13> 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;
}

} // namespace leetcode::p0012
11 changes: 11 additions & 0 deletions solutions/0001-0100/0012-integer-to-roman/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <string>

namespace leetcode::p0012 {

// optimized: O(1) time (13 fixed symbols), O(1) space — greedy, largest
// symbol first.
std::string solve(int num);

} // namespace leetcode::p0012
12 changes: 12 additions & 0 deletions solutions/0001-0100/0012-integer-to-roman/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <catch2/catch_test_macros.hpp>

#include "solution.hpp"

TEST_CASE("Problem 12", "[p0012]") {
CHECK(leetcode::p0012::solve(1) == "I");
CHECK(leetcode::p0012::solve(4) == "IV");
CHECK(leetcode::p0012::solve(9) == "IX");
CHECK(leetcode::p0012::solve(58) == "LVIII");
CHECK(leetcode::p0012::solve(1994) == "MCMXCIV");
CHECK(leetcode::p0012::solve(3999) == "MMMCMXCIX");
}
31 changes: 0 additions & 31 deletions swift/12.swift

This file was deleted.

Loading