var str2 = 'abcdcba';
function plalindrome(str){
return str == str.split('').reverse().join('');
}
console.log(plalindrome(str1));//false
console.log(plalindrome(str2));//true
</script>
2.数组去重
思路:利⽤indexOf()a⽅法,在遍历原数组,若⾥⾯的元素第⼀次出现,则放在数组arr1中,遍历完之后,arr1中存放的是⽆重复的新数组
<script type="text/javascript">
var arr = [2,4,2,2,5,6,7,8,9,9,9];
function unique(arr){
var arr1 = [];
for (var i = 0;i < arr.length;i ++){
if(arr1.indexOf(arr[i]) == -1){
arr1.push(arr[i]);
}
}
return arr1;
}
console.log(unique(arr));//[2, 4, 5, 6, 7, 8, 9]
</script>
3.统计⼀个字符串中出现最多的字母
思路:在另外⼀个数组存放原数组每个元素出现的位置次数,且次数跟存放不重复数组的下标对应,然后取出最多的次数,对应的下标就是不重复数组⾥⾯那个出现次数最多的元素的下标
var str1 = "jhadfgskjfajhdewqe";
var arr1 = str1.split('');
console.log(arr1);
function MostUnit(){
var arrA = [];
var arrB = [];
for(var i = 0 ;i <arr1.length; i ++){
if(arrA.indexOf(arr1[i])==-1){
arrA.push(arr1[i]);
arrB.push(1);
}else {
arrB[arrA.indexOf(arr1[i])] ++;
}
}
console.log(arrB)
console.log(arrA[arrB.indexOf(Math.max.apply(Math,arrB))]);
}
MostUnit();//j
</script>
4.冒泡排序:
<script type="text/javascript">
var arr1 = [2,3,45,64,321,3,21,321,31,999];
function bubbleSort(arr) {
for(var i = 0 ;i < arr1.length-1 ;i ++){
for(var j = 0; j < arr1.length - i - 1 ;j ++){
if(arr[j]>arr[j+1]) {
let tem = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tem;
}
}
}
return arr;
}
console.log(bubbleSort(arr1));//[2, 3, 3, 21, 31, 45, 64, 321, 321, 999]
</script>
5.快速排序:
思路:算法参考某个元素值,将⼩于它的值,放到左数组中,⼤于它的值的元素就放到右数组中,然后递归进⾏上⼀次左右数组的操作,返回合并的数组就是已经排好顺序的数组了。
var arr1 = [1,4,765,86,53,87,53,32,6,64,2,3,767,34,1,4,35,6];
function quickSort(arr){
if(arr.length <= 1){
return arr;
}
var leftArr = [];
var rightArr = [];
var q = arr[0];
for(var i = 1;i < arr.length; i++) {
if(arr[i]>q) {
rightArr.push(arr[i]);
}else{
leftArr.push(arr[i]);
}
}
return [].concat(quickSort(leftArr),[q],quickSort(rightArr));
}
console.log(quickSort(arr1));//[1,4,765,86,53,87,53,32,6,64,2,3,767,34,1,4,35,6] </script>
6.不利⽤第三⽅变量的情况下交换两个变量的值
思路:利⽤两个元素的差值进⾏计算
<script type="text/javascript">
var a = 10;
var b = 12;
function swap (a,b) {
b = b - a;
a = a + b;
b = a - b;
return [a,b]
}
console.log(swap(a,b));
</script>
7.求⼀个数组中最⼤数和最⼩数的差值
<script type="text/javascript">
var arr1 = [2,44,3,-12,43,5,8,67,54,32,-211];
var max = Math.max.apply(Math,arr1);
var min = Math.min.apply(Math,arr1);
console.log(max-min);//278
</script>
8.⽣成指定长度的随机字符串
思路:charAt()⽅法,获取元素下标
function randomString(n){
var str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz9876543210" var str2 = "";
for (var i = 0; i < n ; i ++){
str2 += str1.charAt(Math.floor(Math.random()*str1.length));
}
return str2;
}
console.log(randomString(5));
</script>
9.获取⼀个DOM节点下⾯包含某个class名的所有节点
<div id="text">
<div class="cs"></div>
<div class="as"></div>
<p class="cs"></p>
</div>
<script type="text/javascript">
function getClass(node,classname) {
ElementsByClassName) {
ElementsByClassName(classname);
//如果存在该标签就返回
} else {
var elems = ElementsByTagName(node),
defualt = [];
for (var i = 0; i < elems.length; i++) {
/
/遍历所有标签
if(elems[i].className.indexOf(classname) != -1) {
//查相应类名的标签
defualt[defualt.length] = elems[i];
}
}
return defualt;
}
}
var text = ElementById('text'),
cs = getClass(text,'cs');
console.log(cs);//[div.cs, p.cs]
</script>
10.利⽤javascript实现⼆叉树查
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论