1字符串回文判断
思路:回文是指把相同的词汇或句子,在下文中调换位置或颠倒过来,产生首尾回环的情景,叫做回文,也叫回环。
将一个字符串首尾倒序排列,如果与原字符串相等,则这个字符串回文。
<script type="text/javascript">
var str1 = 'abcdefgh';
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.统计一个字符串中出现最多的字母
思路:在另外一个数组存放原数组每个元素出现的位置次数,且次数跟存放不重复数组的下标对应,然后取出最多的次数,对应的下标就是不重复数组里面那个出现次数最多的元素的下标
<script type="text/javascript">
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.快速排序:
思路:算法参考某个元素值,将小于它的值,放到左数组中,大于它的值的元素就放到右数组中,然后递归进行上一次左右数组的操作,返回合并的数组就是已经排好顺序的数组了。
<script type="text/javascript">
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()方法,获取元素下标
<script type="text/javascript">
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) {
if(node.getElementsByClassName) {
return node.getElementsByClassName(classname);
//如果存在该标签 就返回
} else {
var elems = node.getElementsByTagName(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 = document.getElementById('text'),
cs = getClass(text,'cs');
console.log(cs);//[div.cs, p.cs]
</script>
10.利用javascript实现二叉树查找
由于内容较多,请跳转此文:http://blog.csdn.net/weixin_36185028/article/details/53967888
原文地址:http://www.cnblogs.com/MythLeige/p/6047838.html,有删改