Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add answer to Question1_2 in Java #149

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions c++/Chapter 11/Chapter11_6_MD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Program: Finding a number in a matrix ordered in ascendent order by row and by column;
//

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>

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;
}

40 changes: 40 additions & 0 deletions c++/Chapter 5/Question5_1-MD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdint>
#include <unordered_map>

using namespace std;

string toBinary(int n){
int bits = static_cast<int>(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;
}
Binary file added c++/Chapter 5/main
Binary file not shown.
50 changes: 50 additions & 0 deletions c++/mysolutions/10.1MergeSortedArrays.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <unordered_map>
#include <memory>
#include <set>
#include <algorithm>

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;
}

137 changes: 137 additions & 0 deletions c++/mysolutions/4.8FirstCommonAncestor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <unordered_map>
#include <memory>
#include <set>

using namespace std;

template<class T>
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<T>(val);
return left;
}
Node *addRightNode(const T &val) {
right = new Node<T>(val);
return right;
}
};

Node<char> *loadTree() {
Node<char> *root = new Node<char>('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<class T>
void printInOrder(Node<T> *node) {
if (node == nullptr)
return;

printInOrder(node->left);
cout << node->data << ", ";
printInOrder(node->right);
}

template<class T>
void printPreOrder(const Node<T> *node) {
if (node == nullptr)
return;

cout << node->data << ", ";
printPreOrder(node->left);
printPreOrder(node->right);
}

template<class T>
bool lookFor(const Node<T> *node, const T &val, vector<const Node<T>*> &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<class T>
vector<const Node<T>*> findPath(const Node<T> *node, const T &val) {
vector<const Node<T>*> path;
lookFor(node, val, path);
return path;
}


template<class T>
const Node<T>* getFirstCommonAncestor(const Node<T> *root, const T &val1, const T&val2) {
vector<const Node<T>*> path1 = findPath(root, val1);
vector<const Node<T>*> 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<Node<char>> root(loadTree());
printPreOrder(root.get());
cout << endl << endl;

char val1 = 'd', val2 = 'g';
const Node<char> *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;
}

35 changes: 35 additions & 0 deletions c++/mysolutions/5.6BitFlips.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <unordered_map>
#include <memory>
#include <set>
#include <algorithm>

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;
}

26 changes: 26 additions & 0 deletions java/Chapter 1/Question1_2/Question.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

}