leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 438f235cbc768a2f9e2e2a14db1ea8151bf10f7f
parent 139d1e6bd32f6003fb80e9af1e7cedbbbe0b043e
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 14 Feb 2023 15:56:14 +0100

Random Bit Manipulation Problems

Diffstat:
AProblems/0007.cpp | 19+++++++++++++++++++
AProblems/0268.cpp | 8++++++++
AProblems/0371.cpp | 12++++++++++++
MREADME.md | 3+++
4 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/Problems/0007.cpp b/Problems/0007.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int reverse(int x) { + if (x == INT_MIN || x == INT_MAX) return 0; + + bool neg = false; + unsigned long res = 0; + + if (x < 0) { + neg = true; + x = -x; + } + + do { res = res * 10 + x % 10; } while ((x /= 10) > 0); + + if (res > INT_MAX) return 0; + return !neg ? res : -res; + } +}; diff --git a/Problems/0268.cpp b/Problems/0268.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + int missingNumber(vector<int> &nums) { + int res = nums.size(); + for (int i = 0; i < nums.size(); i++) res ^= i ^ nums[i]; + return res; + } +}; diff --git a/Problems/0371.cpp b/Problems/0371.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int getSum(int a, int b) { + unsigned c; + while (b) { + c = a & b; + a ^= b; + b = c << 1; + } + return a; + } +}; diff --git a/README.md b/README.md @@ -30,6 +30,7 @@ for solving problems. | 0003 | Medium | [Longest Substring Without Repeating Characters](Problems/0003.cpp) | | 0005 | Medium | [Longest Palindromic Substring](Problems/0005.cpp) | | 0006 | Medium | [Zigzag Conversion](Problems/0006.cpp) | +| 0007 | Medium | [Reverse Integer](Problems/0007.cpp) | | 0011 | Medium | [Container With Most Water](Problems/0011.cpp) | | 0012 | Medium | [Integer to Roman](Problems/0012.cpp) | | 0013 | Easy | [Roman to Integer](Problems/0013.cpp) | @@ -175,6 +176,7 @@ for solving problems. | 0257 | Easy | [Binary Tree Paths](Problems/0257.cpp) | | 0263 | Easy | [Ugly Number](Problems/0263.cpp) | | 0264 | Medium | [Ugly Number II](Problems/0264.cpp) | +| 0268 | Easy | [Missing Number](Problems/0268.cpp) | | 0278 | Easy | [First Bad Version](Problems/0278.cpp) | | 0279 | Medium | [Perfect Squares](Problems/0279.cpp) | | 0283 | Easy | [Move Zeroes](Problems/0283.cpp) | @@ -198,6 +200,7 @@ for solving problems. | 0347 | Medium | [Top K Frequent Elements](Problems/0347.cpp) | | 0350 | Easy | [Intersection of Two Arrays II](Problems/0350.cpp) | | 0352 | Hard | [Data Stream as Disjoint Intervals](Problems/0352.cpp) | +| 0371 | Medium | [Sum of Two Integers](Problems/0371.cpp) | | 0374 | Easy | [Guess Number Higher or Lower](Problems/0374.cpp) | | 0376 | Medium | [Wiggle Subsequence](Problems/0376.cpp) | | 0377 | Medium | [Combination Sum IV](Problems/0377.cpp) |