From 545f5ac370319855a31ba810c199a5169d6cf062 Mon Sep 17 00:00:00 2001 From: Michael Duarte Date: Mon, 14 Mar 2016 21:03:29 -0700 Subject: [PATCH 1/2] Add answer to Question1_2 in Java --- java/Chapter 1/Question1_2/Question.java | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 java/Chapter 1/Question1_2/Question.java diff --git a/java/Chapter 1/Question1_2/Question.java b/java/Chapter 1/Question1_2/Question.java new file mode 100644 index 00000000..a2eece74 --- /dev/null +++ b/java/Chapter 1/Question1_2/Question.java @@ -0,0 +1,26 @@ +package Question1_2; + +class Question { + + public static String reverse(String str) { + int length = str.length(); + StringBuilder output = new StringBuilder(length); + for(int i = length-1; i >= 0; --i) + output.append(str.charAt(i)); + return output.toString(); + } + + public static String reverse2(String str) { + StringBuilder output = new StringBuilder(str); + return output.reverse().toString(); + } + + public static void main(String[] args) { + String[] words = {"abcde", "cat"}; + for (String word : words) { + System.out.println("Reversing the string: "+word); + System.out.println("reverse of input string is: " + reverse2(word)); + } + } + +} \ No newline at end of file From e9ca0b89acdbbaf028e41dbcd93071dd31aadfcc Mon Sep 17 00:00:00 2001 From: Michael Duarte Date: Sat, 15 Dec 2018 15:49:32 -0800 Subject: [PATCH 2/2] adds multiple solution files. --- c++/Chapter 11/Chapter11_6_MD.cpp | 52 ++++++++ c++/Chapter 5/Question5_1-MD.cpp | 40 ++++++ c++/Chapter 5/main | Bin 0 -> 14706 bytes c++/mysolutions/10.1MergeSortedArrays.cpp | 50 ++++++++ c++/mysolutions/4.8FirstCommonAncestor.cpp | 137 +++++++++++++++++++++ c++/mysolutions/5.6BitFlips.cpp | 35 ++++++ 6 files changed, 314 insertions(+) create mode 100644 c++/Chapter 11/Chapter11_6_MD.cpp create mode 100644 c++/Chapter 5/Question5_1-MD.cpp create mode 100644 c++/Chapter 5/main create mode 100644 c++/mysolutions/10.1MergeSortedArrays.cpp create mode 100644 c++/mysolutions/4.8FirstCommonAncestor.cpp create mode 100644 c++/mysolutions/5.6BitFlips.cpp diff --git a/c++/Chapter 11/Chapter11_6_MD.cpp b/c++/Chapter 11/Chapter11_6_MD.cpp new file mode 100644 index 00000000..92c924be --- /dev/null +++ b/c++/Chapter 11/Chapter11_6_MD.cpp @@ -0,0 +1,52 @@ +// Program: Finding a number in a matrix ordered in ascendent order by row and by column; +// + +#include "stdafx.h" +#include +#include +#include +#include +#include + +using namespace std; +#define MAX_ELEMENTS 4 + +int matrix[MAX_ELEMENTS][MAX_ELEMENTS] = { + { 1, 5, 8, 11 }, + { 2, 7, 10, 13 }, + { 3, 9, 12, 15 }, + { 4, 11, 14, 17 } }; + +bool findNumInMatrix(int matrix[MAX_ELEMENTS][MAX_ELEMENTS], int size, int x) { + int row = 0; + int column = size - 1; + while (row < size && column >= 0) { + int value = matrix[row][column]; + if (value == x) { + return true; // found + } + //cout << "\t" << x; + if (value < x) { + ++row; // try a row with greater values + //cout << " > " << value << " going to next row" << endl; + } + else if (value > x) { + --column; // try a column with minor values + //cout << " < " << value << " going to previous column" << endl; + } + } + return false; +} + +void evaluate(int values[], int length) { + for (int ind = 0; ind < length; ++ind) + cout << "Value \'" << values[ind] << "\': " << + (findNumInMatrix(matrix, MAX_ELEMENTS, values[ind]) ? "Found" : "Not found") << endl; +} + +int main() { + int values[6] = { 12, 17, 20, 0, 11, 4 }; + evaluate(values, 6); + return 0; +} + diff --git a/c++/Chapter 5/Question5_1-MD.cpp b/c++/Chapter 5/Question5_1-MD.cpp new file mode 100644 index 00000000..6d2e30f1 --- /dev/null +++ b/c++/Chapter 5/Question5_1-MD.cpp @@ -0,0 +1,40 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +string toBinary(int n){ + int bits = static_cast(floor(log2(n)))+1; + string str(bits, '0'); + for (int i = bits-1, j = 0; j < bits; --i, ++j){ + str[i] = (n & (1 << j)) == 0 ? '0' : '1' ; + } + return str; +} + +int main(){ + std::ios::sync_with_stdio(false); // http://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio + cin.tie(NULL); + + int N, M, i, j; // good examples: N = 1024 or 1041, M = 19, i = 2, j = 6. + cin >> N >> M >> i >> j; + + cout << "N = " << toBinary(N) <<".\tM = " << toBinary(M) << endl; + + M <<= i; + int mask = (~0 << j+1) | (1 << i)-1; // clear space for M + N &= mask; + N |= M; + + cout << N << " = " << toBinary(N) << endl; + + return 0; +} \ No newline at end of file diff --git a/c++/Chapter 5/main b/c++/Chapter 5/main new file mode 100644 index 0000000000000000000000000000000000000000..2417ac7d1296b9b36c12a91a7238a7e0745252e9 GIT binary patch literal 14706 zcmeHOeQ;A(cE6G^7y`0QAR#0h6xi$nSrmWam}Igm+dPn1172)`mxMgalCagtk|RBV zO_!9`ONz!z%It1;XlFZRcD9}T)0Wvk2xVw6fdR73Y@I+DrrT^i`CueSS}#ken?l^* zx$hqN=~>#zY}=Xs!JU!LJ-_pD&;5QM_x*v^y3Xlv2u@D%bwOO^JfDQje<3_eC9AMT zFU$US+nPJ*t!h$K=eG7IJ=NCeQ>-{$a&XG@0lk8R>GrVMIC~aV&5U_5gm1@k{+^mh+K2H)k)`-fdlQ zcy7yJ;Duv=i>BK^Rg)ITDNauTVlqog;I*hoF+bZ&;9o}tis}ELg#ODV@VXN5%q-#m z2=t4U`%(#fa|!=lCH$W%;pb`z{R<`VXo+&)DdGR!5`Ji&6|3*hO8Dt3q5mfIt1!C8 zJYC3P45B42*rjiY^h8reB-s)Rrc#lV&;uPC^>8Gq;Es(gu|z!55$ujd zOj&V>-f#3KBf+r#LS#@6A!k5+`=<8P+8S+_%o&Z*L`v@trXux$c+|+_sg@e8FSO;G z?ZH4ulbVzPXqu&>16m*8cB3v7jZ1knnbNjIsWJltdVeIDO2mV)s4=MTs+NlNp+wq{ znu%CSgCdM$sX)6?Qxob9CUqkjj2bBvqHO_IhmfKnJ&`gHbzdOVp=<4{^|o(@q=%`f zYgVTQ;~{-_)aXS6hNB6s+tgTNIza6Lp(16}8PSN=)}D}I)JNjsSW&eip>*g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%LZp%~XHrPA`9ZIETzX|s7 z13$O*DqX-_VXao5v zgIy(@O6a|qH}pjY1x;T_*mdL~dm3Km6xHzrmdDut-q!-QLJ(QNo&Qua#W7J|gr_cySq$iRL z;Mz*v1TAwQomd-%TrIqIP7 literal 0 HcmV?d00001 diff --git a/c++/mysolutions/10.1MergeSortedArrays.cpp b/c++/mysolutions/10.1MergeSortedArrays.cpp new file mode 100644 index 00000000..40533627 --- /dev/null +++ b/c++/mysolutions/10.1MergeSortedArrays.cpp @@ -0,0 +1,50 @@ +// Conversion: write a function to determine the number of bits you would need +// to flip the convert integer A to interget B. +// Example: 29 (11101) to 15 (01111) = 2 + +//#include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +void mergeSortedArrays(int arrayA[], size_t lenA, int arrayB[], size_t lenB) { + size_t totalLength = lenA + lenB; + --lenA; + --lenB; + for (size_t i = totalLength - 1; i >= 0 && i < totalLength; --i) { + if (arrayA[lenA] > arrayB[lenB]) { + arrayA[i] = arrayA[lenA--]; + } else { + arrayA[i] = arrayB[lenB--]; + } + } +} + +void printArray(int arr[], size_t len) { + for (int i = 0; i < len; ++i) { + cout << arr[i] << ", "; + } + cout << endl; +} + +int main() { + int a[15] = { 1,2,3,7,8,9,12 }; + int b[] = { 3,4,5, 6,10,11 }; + int lenA = 7, lenB = sizeof(b) / sizeof(int); + printArray(a, lenA); + printArray(b, lenB); + + mergeSortedArrays(a, lenA, b, lenB); + printArray(a, lenA+lenB); + + return EXIT_SUCCESS; +} + diff --git a/c++/mysolutions/4.8FirstCommonAncestor.cpp b/c++/mysolutions/4.8FirstCommonAncestor.cpp new file mode 100644 index 00000000..69e8c1fc --- /dev/null +++ b/c++/mysolutions/4.8FirstCommonAncestor.cpp @@ -0,0 +1,137 @@ +//#include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +template +struct Node { + T data; + Node *left = nullptr, *right = nullptr; + + Node(const T &val) : data(val) { } + ~Node() { + if (left) { + delete left; + left = nullptr; + } + if (right) { + delete right; + right = nullptr; + } + } + Node *addLeftNode(const T &val) { + left = new Node(val); + return left; + } + Node *addRightNode(const T &val) { + right = new Node(val); + return right; + } +}; + +Node *loadTree() { + Node *root = new Node('a'); + auto b = root->addLeftNode('b'); + auto c = root->addRightNode('c'); + + auto d = b->addLeftNode('d'); + auto e = b->addRightNode('e'); + + e->addLeftNode('f'); + e->addRightNode('g'); + + auto h = c->addLeftNode('h'); + auto i = c->addRightNode('i'); + + h->addLeftNode('j'); + + return root; +} + +template +void printInOrder(Node *node) { + if (node == nullptr) + return; + + printInOrder(node->left); + cout << node->data << ", "; + printInOrder(node->right); +} + +template +void printPreOrder(const Node *node) { + if (node == nullptr) + return; + + cout << node->data << ", "; + printPreOrder(node->left); + printPreOrder(node->right); +} + +template +bool lookFor(const Node *node, const T &val, vector*> &path) { + if (node == nullptr) { + return false; + } + path.push_back(node); + if (node->data == val) { + return true; + } + + if (lookFor(node->left, val, path)) + return true; + if (lookFor(node->right, val, path)) + return true; + + path.pop_back(); + return false; +} + +template +vector*> findPath(const Node *node, const T &val) { + vector*> path; + lookFor(node, val, path); + return path; +} + + +template +const Node* getFirstCommonAncestor(const Node *root, const T &val1, const T&val2) { + vector*> path1 = findPath(root, val1); + vector*> path2 = findPath(root, val2); + int minPathLength = path1.size() < path2.size() ? path1.size() : path2.size(); + + for (int i = 0; i < minPathLength; ++i) { + if (path1[i]->data != path2[i]->data) { + return i > 0 ? path1[i - 1] : nullptr; + } + } + return nullptr; +} + + +int main() { + shared_ptr> root(loadTree()); + printPreOrder(root.get()); + cout << endl << endl; + + char val1 = 'd', val2 = 'g'; + const Node *antecestor = getFirstCommonAncestor(root.get(), val1, val2); + char defaultVal = char(); + if (antecestor != nullptr) { + cout << "First Common Ancestor of node: \'" << val1 << "\' and \'" << val2 << "\' is: \t" << antecestor->data << endl; + } + else { + cout << "No common ancestor found" << endl; + } + + return EXIT_SUCCESS; +} + diff --git a/c++/mysolutions/5.6BitFlips.cpp b/c++/mysolutions/5.6BitFlips.cpp new file mode 100644 index 00000000..abbcb6a3 --- /dev/null +++ b/c++/mysolutions/5.6BitFlips.cpp @@ -0,0 +1,35 @@ +// Conversion: write a function to determine the number of bits you would need +// to flip the convert integer A to interget B. +// Example: 29 (11101) to 15 (01111) = 2 + +// #include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int numberOfFlips(int A, int B) { + int bitIndex = 0; + int flips = 0; + int numBits = std::max(ceil(log2(A)), ceil(log2(B))); + for (int i = 0; i < numBits; ++i) { + int a = A & (1 << i); + int b = B & (1 << i); + if (a != b) + ++flips; + } + return flips; +} + +int main() { + cout << numberOfFlips(29, 15) << endl; + return EXIT_SUCCESS; +} +