Skip to content

Instantly share code, notes, and snippets.

View ivycheung1208's full-sized avatar

Jingyi Zhang ivycheung1208

View GitHub Profile
@ivycheung1208
ivycheung1208 / 17_13.cpp
Last active August 29, 2015 14:05
CC150 17.13
/* CC150 17.13 */
// convert a binary search tree (implemented with BiNode) into a doubly linked list
// the values should be kept in order and the operation should be performed in place
#include <iostream>
using namespace std;
struct BiNode {
BiNode() : data(0), node1(nullptr), node2(nullptr) {}
@ivycheung1208
ivycheung1208 / 17_12.cpp
Last active August 29, 2015 14:05
CC150 17.12
/* CC150 17.12 */
// find all pairs of integers within an array which sum to a specified value
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<pair<int, int>> findPairs(vector<int> arr, int sum)
@ivycheung1208
ivycheung1208 / 17_11.cpp
Last active August 29, 2015 14:05
CC150 17.11
/* CC150 17.11 */
// implement a method rand7() given rand5()
#include <iostream>
#include <cstdlib>
#include <map>
#define N 10000
using namespace std;
@ivycheung1208
ivycheung1208 / 17_9.cpp
Created August 18, 2014 21:16
CC150 17.9
/* CC150 17.9 */
// find the frequency of occurrences of any given word in a book
#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>
using namespace std;
@ivycheung1208
ivycheung1208 / 17_8.cpp
Last active August 29, 2015 14:05
CC150 17.8
/* CC150 17.8 */
// find the contiguous sequence with the largest sum, return the sum
// book solution
#include <iostream>
#include <vector>
using namespace std;
// what if required to return start and end indices of the sequence?!!
@ivycheung1208
ivycheung1208 / 17_7.cpp
Last active August 29, 2015 14:05
CC150 17.7
/* CC150 17.7 */
// print an English phrase that describes the given integer
// http://en.wikipedia.org/wiki/English_numerals
#include <iostream>
#include <string>
using namespace std;
string digits[] = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
@ivycheung1208
ivycheung1208 / 17_6.cpp
Created August 18, 2014 19:39
CC150 17.6
/* CC150 17.6 */
// find the smallest sequence m through n s.t. by sorting this sequence the entire array would be sorted
#include <iostream>
#include <vector>
using namespace std;
pair<int, int> findSeq(vector<int> arr)
{
@ivycheung1208
ivycheung1208 / 17_5.cpp
Created August 18, 2014 14:53
CC150 17.5
/* CC150 17.5 */
// the game of master mind (RGBY)
#include <iostream>
#define N 4
class Result {
public:
Result() : hits(0), phits(0) {}
Result(int h, int p) : hits(h), phits(p) {}
@ivycheung1208
ivycheung1208 / 17_4.cpp
Created August 18, 2014 13:44
CC150 17.4
/* CC150 17.4 */
// find the maximum of two numbers without if-else or any comparison operator
#include <iostream>
int sign(int n) { return (n >> 31) & 0x1 ^ 1; } // 1 for positive and 0 for negative
int maxNumber(int a, int b)
{
// if ab < 0, return the positive one, filt = sign(a)
@ivycheung1208
ivycheung1208 / 17_3.cpp
Last active August 29, 2015 14:05
CC150 17.3
/* CC150 17.3 */
// compute the number of tailing zeros in n factorial
#include <iostream>
int tailZeros(int n)
{
int count = 0;
for (int fact = 5; fact <= n; fact += 5) {
for (int j = fact; j % 5 == 0; j /= 5)