From 6909193d142b726a5a6bf938f0aa2a3c137e8242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:12:40 -0300 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=9F=A1=20Port=20problem=2012=20(Integ?= =?UTF-8?q?er=20to=20Roman)=20from=20Clojure/Swift=20to=20C++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces clojure/12.clj and swift/12.swift with solutions/0001-0100/0012-integer-to-roman/. Both used the same greedy symbol-table algorithm, so only one approach was ported instead of two redundant ones. --- clojure/12.clj | 45 ------------------- .../0001-0100/0012-integer-to-roman/README.md | 12 +++++ .../0012-integer-to-roman/solution.cpp | 26 +++++++++++ .../0012-integer-to-roman/solution.hpp | 11 +++++ .../0001-0100/0012-integer-to-roman/test.cpp | 12 +++++ swift/12.swift | 31 ------------- 6 files changed, 61 insertions(+), 76 deletions(-) delete mode 100644 clojure/12.clj create mode 100644 solutions/0001-0100/0012-integer-to-roman/README.md create mode 100644 solutions/0001-0100/0012-integer-to-roman/solution.cpp create mode 100644 solutions/0001-0100/0012-integer-to-roman/solution.hpp create mode 100644 solutions/0001-0100/0012-integer-to-roman/test.cpp delete mode 100644 swift/12.swift diff --git a/clojure/12.clj b/clojure/12.clj deleted file mode 100644 index 8933337..0000000 --- a/clojure/12.clj +++ /dev/null @@ -1,45 +0,0 @@ -(ns clojure.12 - "Leetcode: 12\n - Name: Integer to Roman\n" - (:require [clojure.test :refer [deftest is run-tests]])) - -(defonce roman-symbols - [["M" 1000] ["CM" 900] ["D" 500] ["CD" 400] ["C" 100] ["XC" 90] - ["L" 50] ["XL" 40] ["X" 10] ["IX" 9] ["V" 5] ["IV" 4] ["I" 1]]) - -(defn int-to-roman - "Convert an integer to its Roman numeral representation. - - Args: - num - An integer value to convert (1 <= num <= 3999). - - Returns: - A string representing the Roman numeral equivalent of the integer. - - Examples: - (int-to-roman 4) ;=> \"IV\" - (int-to-roman 9) ;=> \"IX\" - (int-to-roman 1994) ;=> \"MCMXCIV\"" - [num] - {:pre [(and (integer? num) (<= 1 num 3999))]} - (loop [number num - result ""] - (if (zero? number) - result - (let [[symbol value] (first (filter #(<= (second %) number) roman-symbols))] - (recur (- number value) - (str result symbol)))))) - -(deftest test-int-to-roman - (is (= (int-to-roman 1) "I")) - (is (= (int-to-roman 4) "IV")) - (is (= (int-to-roman 9) "IX")) - (is (= (int-to-roman 58) "LVIII")) - (is (= (int-to-roman 1994) "MCMXCIV")) - (is (= (int-to-roman 3999) "MMMCMXCIX")) - (is (= (int-to-roman 1) "I")) - (is (= (int-to-roman 3999) "MMMCMXCIX")) - (is (thrown? AssertionError (int-to-roman 0))) - (is (thrown? AssertionError (int-to-roman 4000)))) - -(run-tests) diff --git a/solutions/0001-0100/0012-integer-to-roman/README.md b/solutions/0001-0100/0012-integer-to-roman/README.md new file mode 100644 index 0000000..f639646 --- /dev/null +++ b/solutions/0001-0100/0012-integer-to-roman/README.md @@ -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. diff --git a/solutions/0001-0100/0012-integer-to-roman/solution.cpp b/solutions/0001-0100/0012-integer-to-roman/solution.cpp new file mode 100644 index 0000000..e9c48fb --- /dev/null +++ b/solutions/0001-0100/0012-integer-to-roman/solution.cpp @@ -0,0 +1,26 @@ +#include "solution.hpp" + +#include +#include + +namespace leetcode::p0012 { + +std::string solve(int num) { + static constexpr std::array, 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 diff --git a/solutions/0001-0100/0012-integer-to-roman/solution.hpp b/solutions/0001-0100/0012-integer-to-roman/solution.hpp new file mode 100644 index 0000000..5703a5f --- /dev/null +++ b/solutions/0001-0100/0012-integer-to-roman/solution.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +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 diff --git a/solutions/0001-0100/0012-integer-to-roman/test.cpp b/solutions/0001-0100/0012-integer-to-roman/test.cpp new file mode 100644 index 0000000..c5f789d --- /dev/null +++ b/solutions/0001-0100/0012-integer-to-roman/test.cpp @@ -0,0 +1,12 @@ +#include + +#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"); +} diff --git a/swift/12.swift b/swift/12.swift deleted file mode 100644 index 59f6cfb..0000000 --- a/swift/12.swift +++ /dev/null @@ -1,31 +0,0 @@ -class Solution { - func intToRoman(_ num: Int) -> String { - let romanSymbols = [ - ("M", 1000), - ("CM", 900), - ("D", 500), - ("CD", 400), - ("C", 100), - ("XC", 90), - ("L", 50), - ("XL", 40), - ("X", 10), - ("IX", 9), - ("V", 5), - ("IV", 4), - ("I", 1) - ] - - var number = num - var result = "" - - for (symbol, value) in romanSymbols { - while number >= value { - result += symbol - number -= value - } - } - - return result - } -} From 9ca51dd62c33ad35f31e73a385f9f049d86c35bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:13:57 -0300 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20clang-format:=20single=20spa?= =?UTF-8?q?ce=20before=20namespace=20closing=20comment,=20brace-list=20spa?= =?UTF-8?q?cing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0012-integer-to-roman/solution.cpp | 17 ++++++++++++++--- .../0012-integer-to-roman/solution.hpp | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/solutions/0001-0100/0012-integer-to-roman/solution.cpp b/solutions/0001-0100/0012-integer-to-roman/solution.cpp index e9c48fb..49bb2aa 100644 --- a/solutions/0001-0100/0012-integer-to-roman/solution.cpp +++ b/solutions/0001-0100/0012-integer-to-roman/solution.cpp @@ -7,8 +7,19 @@ namespace leetcode::p0012 { std::string solve(int num) { static constexpr std::array, 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"}, + {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; @@ -23,4 +34,4 @@ std::string solve(int num) { return result; } -} // namespace leetcode::p0012 +} // namespace leetcode::p0012 diff --git a/solutions/0001-0100/0012-integer-to-roman/solution.hpp b/solutions/0001-0100/0012-integer-to-roman/solution.hpp index 5703a5f..cc2f7a2 100644 --- a/solutions/0001-0100/0012-integer-to-roman/solution.hpp +++ b/solutions/0001-0100/0012-integer-to-roman/solution.hpp @@ -8,4 +8,4 @@ namespace leetcode::p0012 { // symbol first. std::string solve(int num); -} // namespace leetcode::p0012 +} // namespace leetcode::p0012