zhengliangjun
BAN USERSorted array and insert into array1 ,cost time is n*log(n)+n,but usually the method is not vary fast.
We can designer a simple getHashCode method.
If got hashCode conflict, create new an array named buckets store others.
var arr = ['a','1','b','2','c','3','d','1','1','','','','','','','','','','','','','',''];
var p = arr.length - 1;
for(var i=p;i>=0;i--){
//getNumber
var n = 0;
var nlen = 0;
while(arr[i] != '' && arr[i] < 'a' && i >=0){
if(nlen>0) n+= Math.pow(10,nlen);
else n += parseInt(arr[i]);
arr[i]='';
i--;
nlen++;
}
if(n==0) continue;
var c = arr[i];
arr[i] = '';
console.log(n);
console.log(c);
for(var j=0;j<n;j++){
arr[p] = c;
p--;
}
console.log(arr);
}
console.log(arr);
my code has a bug that is the space at right must be enough long...
- zhengliangjun February 28, 2013var arr = [-1, 4, 6, 7, -9, 5, -3, -4, -7, 8, -6];
console.log(arr);
for(var i = 0,j = arr.length;i < j;i++){
if(arr[i] >= 0){
var last = arr[j];
if(last < 0){
arr[j] = arr[i];
arr[i] = last;
console.log(arr);
}else{
i--;
}
j--;
}
}
console.log(arr);
[-1, 4, 6, 7, -9, 5, -3, -4, -7, 8, -6]
[-1, -6, 6, 7, -9, 5, -3, -4, -7, 8, 4]
[-1, -6, -7, 7, -9, 5, -3, -4, 6, 8, 4]
[-1, -6, -7, -4, -9, 5, -3, 7, 6, 8, 4]
[-1, -6, -7, -4, -9, -3, 5, 7, 6, 8, 4]
create a dictionary<int,bool>
Read every line from secondary and get hashcode and add to dict
then read primary , get one line hash code ,
if(dict.containskey(hashcode)) dict[hashcode] = true;
at last , check dict has false value.
- zhengliangjun March 25, 2013