This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// (1) C#: Defining an initializing a dictionary [key type=string, value type=int] | |
// | |
Dictionary<string, int> dict = new Dictionary<string, int>() | |
{ | |
{"Eve", 101}, | |
{"George", 150}, | |
{"Emma", 200} | |
}; | |
// |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// (1) C++: - Defining a dictionary [key type=string, value type=int] | |
// - No easy way to initialize. | |
// | |
map<string, int> dict; | |
// | |
// (1`) C++11: Defining and initializing a dictionary [key type=string, value type=int] | |
// | |
map<string, int> dict | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tree_node* binary_search(tree_node* n, int value) | |
{ | |
if (n == NULL) | |
{ | |
return NULL; | |
} | |
if (value == n->data) | |
{ | |
return n; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tree_node* binary_search(tree_node* n, int value) | |
{ | |
if (n == NULL) | |
{ | |
return NULL; | |
} | |
tree_node* x = n; | |
while (x != NULL) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tree_node* insert(tree_node* n, int value) | |
{ | |
if (n == NULL) | |
{ | |
return NULL; | |
} | |
tree_node** x = &n; | |
while ((*x) != NULL) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tree_node* insert(tree_node* n, int value) | |
{ | |
if (n == NULL) | |
{ | |
return NULL; | |
} | |
tree_node* x_parent = NULL; | |
tree_node* x = n; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tree_node* insert(tree_node* n, int value) | |
{ | |
if (n == NULL) | |
{ | |
tree_node* new_node = new tree_node(); | |
new_node->data = value; | |
new_node->left = new_node->right = NULL; | |
return new_node; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <fstream> | |
int main() { | |
std::ifstream input_file("/etc/passwd"); | |
if (input_file) | |
{ | |
std::string line; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) { | |
int low = fromIndex; | |
int high = toIndex - 1; | |
while (low <= high) { | |
int mid = (low + high) >>> 1; | |
int midVal = a[mid]; | |
if (midVal < key) | |
low = mid + 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Finds the position of the smallest element in a cyclically sorted array. | |
// Copied verbatim from: http://elementsofprogramminginterviews.com/solutions/java/BinarySearchCircularArray.java | |
// | |
public static int searchSmallest(List<Integer> A) { | |
int left = 0, right = A.size() - 1; | |
while (left < right) { | |
int mid = left + ((right - left) / 2); | |
if (A.get(mid) > A.get(right)) { | |
// Minimum must be in A.subList(mid + 1, right + 1). |
OlderNewer