leetcode

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

commit 6b97a182ad0c8a154b8d9635927778ce6cf328c7
parent 534f9506f11b33e1aff4e245ecbe25a6dbe1588d
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sun, 12 Mar 2023 00:20:25 +0100

Random Problems

Diffstat:
AProblems/0069.cpp | 16++++++++++++++++
AProblems/0168.cpp | 12++++++++++++
AProblems/0171.cpp | 11+++++++++++
MREADME.md | 3+++
4 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/Problems/0069.cpp b/Problems/0069.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int mySqrt(int x) { + int low = 1, high = x; + while (low <= high) { + int mid = low + (high - low) / 2; + if (mid == x / mid) + return mid; + else if (mid > x / mid) + high = mid - 1; + else + low = mid + 1; + } + return high; + } +}; diff --git a/Problems/0168.cpp b/Problems/0168.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + string convertToTitle(int columnNumber) { + string res = ""; + do { + columnNumber -= 1; + res += 'A' + columnNumber % 26; + } while ((columnNumber /= 26)); + reverse(res.begin(), res.end()); + return res; + } +}; diff --git a/Problems/0171.cpp b/Problems/0171.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int titleToNumber(string columnTitle) { + int res = 0; + for (char c : columnTitle) { + res *= 26; + res += c - 'A' + 1; + } + return res; + } +}; diff --git a/README.md b/README.md @@ -76,6 +76,7 @@ for solving problems. | 0064 | Medium | [Minimum Path Sum](Problems/0064.cpp) | | 0066 | Easy | [Plus One](Problems/0066.cpp) | | 0067 | Easy | [Add Binary](Problems/0067.cpp) | +| 0069 | Easy | [Sqrt(x)](Problems/0069.cpp) | | 0070 | Easy | [Climbing Stairs](Problems/0070.cpp) | | 0072 | Hard | [Edit Distance](Problems/0072.cpp) | | 0074 | Medium | [Search a 2D Matrix](Problems/0074.cpp) | @@ -144,7 +145,9 @@ for solving problems. | 0162 | Medium | [Find Peak Element](Problems/0162.cpp) | | 0164 | Hard | [Maximum Gap](Problems/0164.cpp) | | 0167 | Medium | [Two Sum II - Input Array Is Sorted](Problems/0167.cpp) | +| 0168 | Easy | [Excel Sheet Column Title](Problems/0168.cpp) | | 0169 | Easy | [Majority Element](Problems/0169.cpp) | +| 0171 | Easy | [Excel Sheet Column Number](Problems/0171.cpp) | | 0173 | Medium | [Binary Search Tree Iterator](Problems/0173.cpp) | | 0187 | Medium | [Repeated DNA Sequences](Problems/0187.cpp) | | 0189 | Medium | [Rotate Array](Problems/0189.cpp) |