第37篇
1、手写数组快速排序
1 var quickSort = function (arr) { 2 if (arr.length <= 1) { 3 return arr; 4 } 5 6 var pivotIndex = Math.floor(arr.length / 2); 7 var pivot = arr.splice(pivotIndex, 1)[0]; 8 var left = []; 9 var right = []; 10 for (var i = 0; i < arr.length; i++) { 11 if (arr[i] < pivot) { 12 left.push(arr[i]); 13 } else { 14 right.push(arr[i]); 15 } 16 } 17 18 return quickSort(left).concat([pivot], quickSort(right)); 19 }
2、判断一个字符串中出现次数最多的字符,并统计次数
1 var str = "shdshdfjkfjfdgjkjdksgjskdjfsfsfsfjksjkfdkjf"; 2 var arr = str.split("").sort(); //把字符串变为数组,在排序 3 str = arr.join(""); 4 var count = 0; 5 var char = 0; 6 var reg = /(\w)\1+/g; 7 8 str.replace(reg, function (parent, son) { 9 if (parent.length > count) { 10 count = parent.length; 11 char = son; 12 } 13 }); 14 15 console.log("最多的字符为:" + char + ";个数为:" + count);