From bfa8a7f026965de852d4c1678b81bfdc79cbc862 Mon Sep 17 00:00:00 2001 From: Yash Agrawal <38642926+ingenuo-yag@users.noreply.github.com> Date: Tue, 8 Oct 2019 15:45:20 +0530 Subject: [PATCH] Solutions --- AllFactors.cpp | 20 ++++++++++++++++++++ BinaryRepresentation.cpp | 19 +++++++++++++++++++ ExcelColumnNumber.cpp | 14 ++++++++++++++ ExcelColumnTitle.cpp | 26 ++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 AllFactors.cpp create mode 100644 BinaryRepresentation.cpp create mode 100644 ExcelColumnNumber.cpp create mode 100644 ExcelColumnTitle.cpp diff --git a/AllFactors.cpp b/AllFactors.cpp new file mode 100644 index 0000000..b4738c1 --- /dev/null +++ b/AllFactors.cpp @@ -0,0 +1,20 @@ +#include + +vector Solution::allFactors(int num){ + + vector ans; + for(int i = 1; i <= (int)(sqrt(num)); i++){ + if(num%i == 0){ + if(num/i == i){ + ans.push_back(i); + } + else{ + ans.push_back(i); + ans.push_back(num / i); + } + } + } + sort(ans.begin(), ans.end()); + return ans; + +} diff --git a/BinaryRepresentation.cpp b/BinaryRepresentation.cpp new file mode 100644 index 0000000..3068bc2 --- /dev/null +++ b/BinaryRepresentation.cpp @@ -0,0 +1,19 @@ +#include + +string Solution::findDigitsInBinary(int num){ + + string ans = ""; + if(num == 0) + return "0"; + while(num != 1){ + if(num%2 == 0) + ans += "0"; + else + ans += "1"; + num /= 2; + } + ans += "1"; + reverse(ans.begin(), ans.end()); + return ans; + +} diff --git a/ExcelColumnNumber.cpp b/ExcelColumnNumber.cpp new file mode 100644 index 0000000..550fc83 --- /dev/null +++ b/ExcelColumnNumber.cpp @@ -0,0 +1,14 @@ +#include + +int Solution::titleToNumber(string s){ + + int n = s.size(); + int ans = 0; + int cnt = 0; + for(int i = n - 1; i >= 0; i--){ + ans += (pow(26, cnt) * (s[i] - 'A' + 1)); + cnt++; + } + return ans; + +} diff --git a/ExcelColumnTitle.cpp b/ExcelColumnTitle.cpp new file mode 100644 index 0000000..063646a --- /dev/null +++ b/ExcelColumnTitle.cpp @@ -0,0 +1,26 @@ +#include + +string Solution::convertToTitle(int num){ + + unordered_map mp = {{1, 'A'}, {2, 'B'}, {3, 'C'}, {4, 'D'}, {5, 'E'}, + {6, 'F'}, {7, 'G'}, {8, 'H'}, {9, 'I'}, {10, 'J'}, + {11, 'K'}, {12, 'L'}, {13, 'M'}, {14, 'N'}, {15, 'O'}, + {16, 'P'}, {17, 'Q'}, {18, 'R'}, {19, 'S'}, {20, 'T'}, + {21, 'U'}, {22, 'V'}, {23, 'W'}, {24, 'X'}, {25, 'Y'}, {26, 'Z'}}; + string ans = ""; + while(num != 0){ + int rem = num % 26; + if(rem == 0){ + rem = 26; + num /= 26; + num -= 1; + } + else{ + num /= 26; + } + ans += mp[rem]; + } + reverse(ans.begin(), ans.end()); + return ans; + +}