输出结果如下:
a,b,a,c,c,c
a,b,c
a,b,a,c,c,c
a,b,c
解决方案 »
- web开发
- js中有让Date对象的时间+1分钟的方法吗
- 请教大家一个问题,关于JS插入
- 如何使用*{padding:0px;margin:0px;}又保留table的cellpadding
- 如何用Javascript写一个本地页面
- if(document.selection)这个判断条件是不是形同虚设?
- 请问怎样修改ie的onclick事件?
- 如何让select选中后相应的checkbox被选中.
- 怎麼在保持原table內容時在table加入一行<tr></tr>???
- 一个下拉菜单的效果,在本地打开可以运行,在服务器上运行有错!
- [问题] 利用Javascript连接至新窗口
- 需要大家的帮助,一个显示时间的问题!
a.sort()
<!--
var array1=new Array();
for(var i=0; i<1000; i++)
{
array1[array1.length] = String.fromCharCode(Math.floor(Math.random()*26)+97);
}
document.write(array1+"<br>");function getUnique(someArray){
tempArray=someArray.slice(0);//复制数组到临时数组
for(var i=0;i<tempArray.length;i++){
for(var j=i+1;j<tempArray.length;){
if(tempArray[j]==tempArray[i])
//后面的元素若和待比较的相同,则删除并计数;
//删除后,后面的元素会自动提前,所以指针j不移动
{tempArray.splice(j,1);}
else{j++;}
//不同,则指针移动
}
}
return tempArray
}function getUnique2(A) //适用于字符型数组
{
var str = "\x0f"+ A.join("\x0f");
while(/(\w+)[^\1]*\1/.test(str))
str = str.replace("\x0f"+ RegExp.$1, "");
return str.substr(1).split("\x0f");
}
var d = new Date().getTime();
document.write(getUnique(array1));
d = new Date().getTime()-d;
document.write("<br>2000节点 老算法计耗时 "+ d +" 毫秒!<br><br>");var d = new Date().getTime();
document.write(getUnique2(array1));
d = new Date().getTime()-d;
document.write("<br>2000节点 新算法计耗时 "+ d +" 毫秒!");
//-->
</SCRIPT>
var array1=new Array("a","c","b","b","a","c","c","c");Array.prototype.unique = array_unique;function array_unique()
{
var o = new Object();
for (var i=0,j=0; i<this.length; i++)
{
if (typeof o[this[i]] == 'undefined')
{
o[this[i]] = j++;
}
} this.length = 0;
for (var key in o)
{
this[o[key]] = key;
} return this;
}var d = new Date().getTime();
document.write(array1.unique());
d = new Date().getTime()-d;
document.write("<br>2000节点 新算法计耗时 "+ d +" 毫秒!");
</script>
<!--
var array1=new Array();
for(var i=0; i<1000; i++)
{
array1[array1.length] = String.fromCharCode(Math.floor(Math.random()*26)+97);
}
document.write(array1+"<br>");function getUnique(someArray){
tempArray=someArray.slice(0);//复制数组到临时数组
for(var i=0;i<tempArray.length;i++){
for(var j=i+1;j<tempArray.length;){
if(tempArray[j]==tempArray[i])
//后面的元素若和待比较的相同,则删除并计数;
//删除后,后面的元素会自动提前,所以指针j不移动
{tempArray.splice(j,1);}
else{j++;}
//不同,则指针移动
}
}
return tempArray
}function getUnique2(A) //适用于字符型数组
{
var str = "\x0f"+ A.join("\x0f");
while(/(\w+)[^\1]*\1/.test(str))
str = str.replace("\x0f"+ RegExp.$1, "");
return str.substr(1).split("\x0f");
}
Array.prototype.unique = array_unique;function array_unique()
{
var o = new Object();
for (var i=0,j=0; i<this.length; i++)
{
if (typeof o[this[i]] == 'undefined')
{
o[this[i]] = j++;
}
} this.length = 0;
for (var key in o)
{
this[o[key]] = key;
} return this;
}var d = new Date().getTime();
document.write(getUnique(array1));
d = new Date().getTime()-d;
document.write("<br>2000节点 wzhiyuan (我是谁) 算法计耗时 "+ d +" 毫秒!<br><br>");var d = new Date().getTime();
document.write(getUnique2(array1));
d = new Date().getTime()-d;
document.write("<br>2000节点 meizz(梅花雪) 算法计耗时 "+ d +" 毫秒!<br><br>");var d = new Date().getTime();
document.write(array1.unique());
d = new Date().getTime()-d;
document.write("<br>2000节点 关联结构算法计耗时 "+ d +" 毫秒!");
//-->
</SCRIPT>
我的算法比较传统,不过通用,稳定。不管数组的数据如何,结果都一样,时间也稳定(n*n )。
你的算法我研究了一下,好在构思新颖,效率有时候也很高,不过我觉得还有改进的余地。
第一,你用正则替换时,一次替换只是把两个相同的中的第一个替换成空,是不是可以考虑,一次替换中,把相同的只保留一个,其余所有的都替换成空呢?
第二,用你的正则匹配时,效率高低很大程度上取决于目标串的内容,比如数组我用下面的方法构造时,你再测试一下结果!
var array1=new Array();
array1[0]="A";
array1[1]="B";
array1[2]="C";
array1[3]="D";
array1[4]="E";
for(var i=5; i<1000; i++)
{
array1[i] = String.fromCharCode(Math.floor(Math.random()*26)+97);
}第三,你的正则用“\w”匹配,这样对类似“ text”和"text"这样的字串,根本就认为是相同的。以上我的看法有不成熟的地方大哥再指正。TO seeu1688(kangel)
你的我还没细看,待我研究后再讨论。欢迎各位继续探讨。
是我能想象的最强的。时间复杂度是n。个人觉得已经完美了。seeu1688(kangel),请受我一拜!
http://community.csdn.net/Expert/topic/3353/3353124.xml?temp=.4150659
<script>
var array1=[];
for(var i=0; i<1000; i++)
array1[array1.length] = String.fromCharCode(Math.floor(Math.random()*26)+97);
document.write(array1+"<br/>");
var t=new Date().getTime();
s="";
for(i=0;i<array1.length;i++)
if((","+s).indexOf(","+array1[i]+",")==-1)
s+=array1[i]+",";
s=s.substring(0,s.length-1).split(",");
document.write(new Date().getTime()-t);
document.write(s);
</script>
2000节点 wzhiyuan (我是谁) 算法计耗时 313 毫秒!a,v,b,q,k,n,s,z,f,x,e,p,o,m,w,d,h,i,l,c,t,r,g,u,y,j
2000节点 meizz(梅花雪) 算法计耗时 62 毫秒!x,y,g,w,t,j,v,f,m,o,l,a,i,d,c,k,q,r,p,z,e,n,h,b,u,s
2000节点 scoutlin(梅川库子)算法计耗时 16 毫秒!x,y,g,w,t,j,v,f,m,o,l,a,i,d,c,k,q,r,p,z,e,n,h,b,u,s
2000节点 关联结构算法计耗时 0 毫秒!seeu1688(kangel)实在是精彩