Skip to content

Instantly share code, notes, and snippets.

View ivycheung1208's full-sized avatar

Jingyi Zhang ivycheung1208

View GitHub Profile
@ivycheung1208
ivycheung1208 / 17_2.cpp
Created August 18, 2014 12:57
CC150 17.2
/* CC150 17.2 */
// determine if someone has won a game of tic-tac-toe
// (expand to a full game!)
#include <iostream>
using namespace std;
// assume only one row satisfying the condition exists since the games ends once a player succeeded
// in placing three respective marks in a horizontal, vertical or diagonal row.
@ivycheung1208
ivycheung1208 / 17_1.cpp
Created August 18, 2014 12:24
CC150 17.1
/* CC150 17.1 */
// swap a number in place
#include <iostream>
void swapInPlace(int &a, int &b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
@ivycheung1208
ivycheung1208 / 13_10.cpp
Created August 17, 2014 21:21
CC150 13.10
/* CC150 13.10 */
// my2DAlloc(int rows, int cols)
#include <iostream>
using namespace std;
int **my2DAlloc(int rows, int cols)
{
int header = rows * sizeof(int*);
@ivycheung1208
ivycheung1208 / 13_9.cpp
Created August 17, 2014 20:32
CC150 13.9
/* CC150 13.9 */
// aligned malloc and free function
// (book solution)
#include <iostream>
void *aligned_malloc(size_t required_bytes, size_t alignment)
{
void *p1; // original block
void **p2; // aligned block
@ivycheung1208
ivycheung1208 / 13_8.cpp
Created August 17, 2014 19:55
CC150 13.8
/* CC150 13.8 */
// smart pointer class
// Template implementation! (book solution)
template <class T> class SmartPointer {
public:
// constructors
SmartPointer(T *ptr) {
ref = ptr;
refCount = (unsigned*)malloc(sizeof(unsigned));
@ivycheung1208
ivycheung1208 / 13_7.cpp
Last active August 29, 2015 14:05
CC150 13.7
/* CC150 13.7 */
// copy Node
#include <map>
using namespace std;
struct Node{
Node *left, *right;
};
@ivycheung1208
ivycheung1208 / 13_1.cpp
Last active August 29, 2015 14:05
CC150 13.1
/* CC150 13.1 */
// print the last K lines of an input file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void printLastLines(string filename, int K)
@ivycheung1208
ivycheung1208 / 11_8.cpp
Created August 14, 2014 02:30
CC150 11.8
/* CC150 11.8 */
// track(int x), getRank(int x)
#include <iostream>
#include <map>
#include <string>
#include <sstream>
using namespace std;
@ivycheung1208
ivycheung1208 / 11_7.cpp
Last active August 29, 2015 14:05
CC150 11.7
/* CC150 11.7 */
// compute the largest possible people tower, givin (ht, wt)
#include <iostream>
#include <vector>
using namespace std;
class Person {
public:
@ivycheung1208
ivycheung1208 / 11_6.cpp
Last active August 29, 2015 14:05
CC150 11.6
/* CC150 11.6 */
// find an element in a M-by-N matrix in which each row and column is sorted in ascending order
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;