Skip to content

Instantly share code, notes, and snippets.

View crimx's full-sized avatar

CRIMX crimx

View GitHub Profile
M[16],X=16,W,k;
main() {
T(system("stty cbreak"));
puts(W&1 ? "WIN":"LOSE");
}
K[]={2,3,1};
s(f,d,i,j,l,P) {
for(i=4; i--; )
for(j=k=l=0; k<4; )
@crimx
crimx / qsort.c
Last active August 29, 2015 13:59
Quick Sort Template
void Qsort(int a[], int s, int e) {
if(s>=e) return;
int p=s, q=e, t;
int k = getPivot(a,s,e);
t = a[k];
a[k] = a[s];
while(p<q) {
while(p<q&&a[q]>=t) q--;
a[p] = a[q];
while(p<q&&a[p]<=t) p++;
@crimx
crimx / insertion_sort.c
Created April 12, 2014 04:52
Insertion sort template
int Isort(int a[], int s, int e) {
int i, j, t;
for(i=s+1; i <= e; i++) {
t = a[i];
for(j = i-1; j>=s && a[j]>t; j--)
a[j+1] = a[j];
a[j+1] = t;
}
}
@crimx
crimx / Singleton pattern.java
Created April 14, 2014 15:13
Singleton pattern
public class Singleton {
private static Singleton instance = null;
private Singleton() {
//dosomething
}
public static synchronized Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
@crimx
crimx / Adapter.java
Created April 14, 2014 16:28
Adapter Pattern
package com.crimx.adapter;
public class Adapter implements Plug2{
private Plug1 plug1;
public Adapter(Plug1 p1) {
Plug1 = p1;
}
public void slot1() {
@crimx
crimx / string reverse.c
Created April 16, 2014 17:03
String Reverse
void swap(char *a, char *b) {
*a = *a^*b;
*b = *a^*b;
*a = *a^*b;
}
void reverse(char *a) {
if(!a) return;
int i, n = -1;
while(a[++n]);
@crimx
crimx / compare.js
Last active August 29, 2015 14:03
slice, substr, substring in JavaScript
String.prototype.slice(start[,end]); If a negative number is given, it is treated as strLength + start/end.
String.prototype.substr(start[,length]); If a negative number is given to start, it is treated as strLength + start.
String.prototype.substring(indexA[,indexB]); If either argument is less than 0 or is NaN, it is treated as if it were 0.
If either argument is greater than stringName.length, it is treated as if it were stringName.length.
var s1 = "0123456789"
console.log(s1.slice(3, 7)); //"3456"
@crimx
crimx / 【算】左旋转字符串.js
Last active August 29, 2015 14:05
定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部。如把字符串 abcdef 左旋转 2 位得到字符串cdefab。请实现字符串左旋转的函数,要求对长度为 n 的字符串操作的时间复杂度为 O(n),空间复杂度为 O(1)。《程序员编程艺术一》
window.myApp = (function (myApp) {
/*
* 123456 --leftShift+2--> 345612
* 1. 12 | 3456
* 2. 21 | 3456
* 3. 21 | 6543
* 4. 216543
* 5. 345621
*/
myApp.leftShift = function (arr, offset) {
@crimx
crimx / 【算】字母包含问题.js
Last active August 29, 2015 14:05
给定一长一短的两个字符串 A,B,假设 A 长 B 短,要判断 A 是否包含了 B 中所有的字母。《程序员编程艺术二》
window.myApp = (function (myApp) {
// O(m+n)
myApp.containChar = function (Str1, Str2) {
if (typeof Str1 !== "string" || typeof Str2 !== "string") {
return;
}
var longStr,
@crimx
crimx / CountingSort.js
Last active August 29, 2015 14:05
对长度10000的数组排序,0< arr[i] < 9999
var arr = [];
for (var i=0; i< 10000; i++) {
arr[i] = Math.floor(Math.random()*9997+1); //1~9998
}
// console.log(a);
function sort(arr) {