之後會記錄本身解決過和遇到過的算法相關的題,系列一就以常見的開篇吧。html
原本想多列幾個排序方法,可是其它都不常見,更多請點擊查看,冒泡排序和快速排序中以快速排序最優。git
var arr = [0, 4, 2, 1, 3]; var i = j = 0; for(i; i<arr.length-1; i++){ for(j; j<=arr.length-i; j++){ var temp = 0; // ">" 從小到大排序 // "<" 從大到小排序 if(arr[j] > arr[j+1]){ temp = arr[j] arr[j] = arr[j+1] arr[j+1] = temp } } } console.log(arr);
function quickSort(arr){ //若是數組<=1,則直接返回 if (arr.length <= 1) { return arr; } var pivotIndex = Math.floor(arr.length/2); //找基準,並把基準從原數組刪除 var pivot = arr.splice(pivotIndex,1)[0]; //定義左右數組 var left = []; var right = []; //比基準小的放在left,比基準大的放在right for (var i=0;i<arr.length;i++) { if (arr[i] <= pivot) { left.push(arr[i]); } else { right.push(arr[i]); } } //遞歸 return [...quickSort(left), pivot, ...quickSort(right)]; } console.log(quickSort([0, 4, 2, 1, 3]));
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2]; for (var i = 0, newArr = [], len = arr.length; i < len; i++){ if (newArr.indexOf(arr[i]) == -1) { newArr.push(arr[i]); } } console.log(newArr);
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2]; for (var i = 1, newArr = [arr[0]], len = arr.length; i < len; i++){ if (arr.indexOf(arr[i]) == i) { newArr.push(arr[i]); } } console.log(newArr);
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2]; for (var i = 0, obj = {}, newArr = [], len = arr.length, val, type; i < len; i++){ val = arr[i]; type = typeof val; if (!obj[val]) { // 對象沒有該鍵對應的值 obj[val] = type; newArr.push(val); } else if (obj[val].indexOf(type) < 0) {//對象有該鍵對應的值且它的值是不重複的 obj[val] = type; newArr.push(val); } } console.log(newArr);
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2]; arr.sort(); var newArr = [arr[0]]; for (var i = 1, len = arr.length; i < len; i++) { if (arr[i] !== newArr[newArr.length-1]) { newArr.push(arr[i]); } } console.log(newArr);
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2]; for (var i = 0, len = arr.length, newArr = []; i < len; i++) { for (var j = i + 1; j < len; j++) { if (arr[i] === arr[j]) j = ++i; } newArr.push(arr[i]); } console.log(newArr);
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2]; for (var i = 0, len = arr.length; i < len; i++) { for (var j = i + 1; j < len; j++) { if (arr[i] === arr[j]) { arr.splice(j, 1); len--; j--; } } } console.log(arr);
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2]; // 7.1 var newArr = [...new Set(arr)]; console.log(newArr); // 7.2 var newArr = Array.from(new Set(arr)); console.log(newArr);
// 數組去重,打亂數組,統計數組各個元素出現的次數, 字符串各個字符的出現次數,獲取地址連接的各個參數
var arr = [2,5,'6',3,9,'2',4,7]; arr.sort(function(){ return 0.5 - Math.random() }); console.log(arr);
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2]; for (var i = 0, len = arr.length, newArr = []; i < len; i++) { var isPush = true; for (var j = 0, l = newArr.length; j < l; j++) { if (arr[i] === newArr[j].val) { isPush = false; newArr[j].count++; } } if (isPush) { newArr.push({val: arr[i], count: 1}); } else { isPush = true; } } console.log(newArr);
var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2]; var newArr = arr.reduce((arrs, cv) => { const found = arrs.find(it => it[0] === cv); if (found) { found[1] += 1; } else { arrs.push([cv, 1]); } return arrs; }, []); console.log(newArr);
var str = 'abdcsdwdaoprr'; for (var i = 0, len = str.length, obj = {}; i < len; i++) { if (obj[str[i]]) { obj[str[i]]++; } else { obj[str[i]] = 1; } } console.log(obj);
var url = 'https://www.google.com.hk/search?safe=strict&ei=M_u5WvLZO4bzUrfpn_AJ&q=js%9F%&oq=js%E7%BB&gs_l=psy-ab'; var search = url.substring(url.indexOf('?')+1); var tmpArr = search.split('&'); var urlParmObj = {}; if (tmpArr.length > 0 && tmpArr[0] != '') { for (var i = 0, len = tmpArr.length; i < len; i++) { var tmp = tmpArr[i].split('='); urlParmObj[tmp[0]] = tmp[1]; } } console.log(urlParmObj);
var arr = [1,2,3,4,5]; const param = arr.find(it => it === 5); param += 1; console.log(arr);
更多其它內容請查看es6