Skip to content

Instantly share code, notes, and snippets.

View crimx's full-sized avatar

CRIMX crimx

View GitHub Profile
@crimx
crimx / 有序比较.js
Created August 26, 2014 14:29
有2个int型已经去重的数组a和b,都是已经从小到大排序好的。要求遍历b数组的数字,如果这个数字出现在a中,就将其从a删去;反之将其插入到a的适当位置,使a保持排序状态。
// O(m+n)
var foo = function(arrA, arrB) {
var iB,
iA;
for (iA = iB = 0; iB < arrB.length; iB += 1) {
while (arrB[iB] > arrA[iA] && iA < arrA.length) {
iA += 1;
}
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>Test by Jesse</title>
</head>
<body>
<nav id = 'sidenav'>
<ul>
@crimx
crimx / 递归寻后代.js
Created August 25, 2014 17:43
编写一个JavasSript函数,给定一个DOM节点node和一个正整数n, 返回node的所有第n代后代节点(直接子节点为第1代)
function getDescendants(node, n) {
var res = [];
function f(children, lev) {
if (lev >= n) {
res = res.concat(Array.prototype.slice.apply(children));
return;
@crimx
crimx / 数组元素左最近最大.js
Last active August 29, 2015 14:05
给定数组Arr[n],对于其中的每个元素Arr[i](0=<i<n),在Arr[0...i-1]中找到元素Arr[k],Arr[k]满足Arr[k]>Arr[i],并且i-k值最小(即最靠近)。 要求O(n)时间内找出Arr中所有元素对应的Arr[k]的位置。
var arr = [],
i;
for (i = 0; i < 10000; i++) {
arr[i] = Math.floor(Math.random()*9999);
}
// console.log(arr);
function findLG(arr) {
@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) {
@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 / 【算】左旋转字符串.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 / 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 / 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 / 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() {