常见前端算法面试题
2021-06-22 11:03
标签:算法 sort console string ons search === data 去重 1.写一个方法将数组换成前端更易解析的树状结构 2.统计字符串出现最多的字母 3.排序算法 4.去重算法 5.二分查找算法(建立在已经排好序的情况下) 常见前端算法面试题 标签:算法 sort console string ons search === data 去重 原文地址:https://www.cnblogs.com/TigerZhang-home/p/9678781.htmlfunction getTree(data) {
var newData = [],
hash = {};
for (var i = 0; i
if (!hash[data[i].province]) {
hash[data[i].province] = {
‘province‘: data[i].province
};
hash[data[i].province][‘city‘] = [{
‘name‘: data[i].city,
‘code‘: data[i].code
}]
newData.push(hash[data[i].province]);
} else if (hash[data[i].province].province == data[i].province) {
hash[data[i].province][‘city‘].push({
‘name‘: data[i].city,
‘code‘: data[i].code
})
}
}
return newData;
}var data = [{
‘province‘: ‘浙江‘,
‘city‘: ‘温州‘,
‘code‘: ‘10010‘
}, { ‘province‘: ‘浙江‘,
‘city‘: ‘杭州‘,
‘code‘: ‘10011‘
}, { ‘province‘: ‘安徽‘,
‘city‘: ‘合肥‘,
‘code‘: ‘10012‘
}, { ‘province‘: ‘安徽‘,
‘city‘: ‘马鞍山‘,
‘code‘: ‘10013‘
}, { ‘province‘: ‘浙江‘,
‘city‘: ‘宁波‘,
‘code‘: ‘10014‘
}];console.log(getTree(data));function getMax(str) {
var hash = {},
maxstr, max = 1;
for (var i = 0; i max) {
max = hash[str[i]];
maxstr = str[i];
}
}
return maxstr;
}
var str = ‘abcdeffggghhhhiiiii‘;
console.log(getMax(str));
function bubbleSort(arr) {
for (var i = 0; i
function unique1(arr) {
var newArr = [];
for (var i = 0; i
function binarySearch(arr, data) {
var end = arr.length - 1,
start = 0;
while (start data) {
end = middle - 1;
} else if (arr[middle]