From fae26676326e3b05128553944e143684cd4d3083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:24:22 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20Port=20problem=20136=20(Single?= =?UTF-8?q?=20Number)=20from=20Swift=20to=20C++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces swift/136.swift with solutions/0101-0200/0136-single-number/. XORs through an unsigned accumulator to avoid signed-bitwise UB concerns on the int inputs. --- .../0101-0200/0136-single-number/README.md | 12 ++++++ .../0101-0200/0136-single-number/solution.cpp | 13 +++++++ .../0101-0200/0136-single-number/solution.hpp | 10 +++++ .../0101-0200/0136-single-number/test.cpp | 8 ++++ swift/136.swift | 37 ------------------- 5 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 solutions/0101-0200/0136-single-number/README.md create mode 100644 solutions/0101-0200/0136-single-number/solution.cpp create mode 100644 solutions/0101-0200/0136-single-number/solution.hpp create mode 100644 solutions/0101-0200/0136-single-number/test.cpp delete mode 100644 swift/136.swift diff --git a/solutions/0101-0200/0136-single-number/README.md b/solutions/0101-0200/0136-single-number/README.md new file mode 100644 index 0000000..e586543 --- /dev/null +++ b/solutions/0101-0200/0136-single-number/README.md @@ -0,0 +1,12 @@ +# 136. Single Number + +[LeetCode problem 136](https://leetcode.com/problems/single-number/) + +- **Difficulty**: Easy +- **Tags**: array, bit-manipulation + +## Description + +Given a non-empty array of integers where every element appears twice except +for one, find that single element. Must run in O(n) time using O(1) extra +space. diff --git a/solutions/0101-0200/0136-single-number/solution.cpp b/solutions/0101-0200/0136-single-number/solution.cpp new file mode 100644 index 0000000..cee0174 --- /dev/null +++ b/solutions/0101-0200/0136-single-number/solution.cpp @@ -0,0 +1,13 @@ +#include "solution.hpp" + +namespace leetcode::p0136 { + +int solve(const std::vector& nums) { + unsigned result = 0; + for (int num : nums) { + result ^= static_cast(num); + } + return static_cast(result); +} + +} // namespace leetcode::p0136 diff --git a/solutions/0101-0200/0136-single-number/solution.hpp b/solutions/0101-0200/0136-single-number/solution.hpp new file mode 100644 index 0000000..0bb1b42 --- /dev/null +++ b/solutions/0101-0200/0136-single-number/solution.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +namespace leetcode::p0136 { + +// optimized: O(n) time, O(1) space — XOR cancels out every paired value. +int solve(const std::vector& nums); + +} // namespace leetcode::p0136 diff --git a/solutions/0101-0200/0136-single-number/test.cpp b/solutions/0101-0200/0136-single-number/test.cpp new file mode 100644 index 0000000..14475fd --- /dev/null +++ b/solutions/0101-0200/0136-single-number/test.cpp @@ -0,0 +1,8 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 136", "[p0136]") { + CHECK(leetcode::p0136::solve({2, 2, 1}) == 1); + CHECK(leetcode::p0136::solve({4, 1, 2, 1, 2}) == 4); +} diff --git a/swift/136.swift b/swift/136.swift deleted file mode 100644 index 5eee177..0000000 --- a/swift/136.swift +++ /dev/null @@ -1,37 +0,0 @@ -func singleNumber(_ nums: [Int]) -> Int { - var number = 0 - - for num in nums { - number ^= num - } - - return number -} - -func assertTrueInt(_ expectedValues: [Int], _ actualValues: [Int]) { - for i in 0..