Skip to content

Instantly share code, notes, and snippets.

View tamarous's full-sized avatar
🎯
Focusing

Tamarous tamarous

🎯
Focusing
View GitHub Profile
@tamarous
tamarous / travel.cpp
Created March 29, 2018 12:19
Pseudo-code for DFS and BFS.
// DFS
void search(Node root) {
if (root == NULL) {
return;
}
visit(root);
root.visited = true;
foreach (Node n in root.adjacent) {
if (n.visited = false) {
search(n);
@tamarous
tamarous / bits.cpp
Last active March 26, 2018 10:08
Common bit operations
// Get the ith bit of num.
bool getBit(int num, int i) {
return (num & (1 << i)) != 0;
}
// Set the ith bit of num.
int setBit(int num, int i) {
return num | (1 << i);
}
@tamarous
tamarous / KthLargestNumber.cpp
Created February 27, 2018 06:38
寻找数组中第k大的元素
int findKthLargest(vector<int>& nums, int k) {
int lo = 0;
int high = (int)nums.size() - 1;
for(; lo < high; ) {
int i = lo, j = high;
int pivot = nums[lo];
while(i < j) {
while(i < j && pivot >= nums[j]) {
j--;
@tamarous
tamarous / KthNumber.cpp
Last active February 27, 2018 06:37
寻找数组中第k小的元素
int quickSelect(vector<int> &nums, int k) {
int lo = 0;
int high = (int)nums.size() - 1;
for(; lo < high; ) {
int i = lo, j = high;
int pivot = nums[lo];
while(i < j) {
while( i < j && pivot <= nums[j]) {
j--;
}
@tamarous
tamarous / sort.cpp
Last active March 29, 2018 13:05
Implementation of different sort algorithms.
// --------------- Selection Sort---------------------//
void selectionSort(vector<int> &num) {
int size = num.size();
for(int i = 0; i < size ; i++) {
int min = i;
for(int j = i + 1; j < size; j++) {
if(num[j] < num[min]) {
min = j;
}
}
@tamarous
tamarous / build_post.cpp
Last active December 25, 2017 16:10
从前序和中序遍历中重建二叉树
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
void build_post(const char *pre, const char *in, const int n, char *post) {
int left_len = strchr(in, pre[0]) - in;
if (n <= 0) {
return;
}
@tamarous
tamarous / GLHelper.cpp
Last active December 24, 2017 03:00
使用OpenGL ES以双目渲染和显示360度视频。博文地址:http://www.tamarous.com/2017/12/23/render-360-video-using-opengles/
#include "GLHelper.h"
#include <android/native_window.h>
const char* TEXTURE_UNIFORMS[] = {"y_tex", "u_tex", "v_tex"};
GLuint yuvTextures[3];
#define glCheckError() glCheckError_(__LINE__)
void glCheckError_(int line)
{
@tamarous
tamarous / GLHelper.h
Last active December 24, 2017 03:01
使用OpenGL ES以双目渲染和显示360度视频。博文地址:http://www.tamarous.com/2017/12/23/render-360-video-using-opengles/
#ifndef GLHELPER_H
#define GLHELPER_H
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <vector>
#include <pthread.h>
#include "MyGLCamera.h"
@tamarous
tamarous / CStopwatch.cpp
Last active December 29, 2017 07:16
一个高精度计时器类,可以用于测量代码运行的时间用以进行性能分析
class CStopwatch {
public:
CStopwatch() {
QueryPerformanceFrequency(&m_liPerfFreq);
}
void Start() {
QueryPerformanceCounter(& m_liPerfStart);
}
// 打印链表中的元素
void Print(Node *head) {
if (head == NULL) {
return;
}
Node *ptr = head->next;
while(ptr != NULL) {
cout << ptr->data << endl;
ptr = ptr->next;