这篇文章继续分享一些公司常问面试题,以供参考。
台阶走法问题
'use strict';
// 递归实现
const recursionCount = function (n) {
if (n <= 0) return 0;
if (n === 1) return 1;
if (n === 2) return 2;
return count(n - 1) + count(n - 2);
};
// 非递归 用数组实现
const unrecursionCount1 = function (n) {
if (n <= 0) return 0;
if (n === 1) return 1;
if (n === 2) return 2;
const steps = [1, 2];
for (let i = 2; i < n; i++) {
steps[i] = a[i - 1] + a[i - 2];
}
return steps[n - 1];
};
// 非递归 用迭代实现
const unrecursionCount2 = function (n) {
let i = 2,
a = 1,
b = 2,
sum = 0;
if (n < 1) {
return -1;
}
for (; i < n; i++) {
sum = a + b;
a = b;
b = sum;
}
return sum;
};
// 打印路径
const printSteps = function (n, preStr) {
if (typeof n !== 'number') throw new Error('not a number');
const str = preStr ? preStr : '';
if (n < 0) {
console.log('can\'t print Steps, n < 0');
return;
}
if (n === 0) {
console.log(str);
return;
}
if (n === 1) {
console.log(`${str} 1`);
return;
}
for (let i = 1; i <= 2; i++) {
printSteps(n - i, `${str} ${i}`);
}
return;
}
printSteps(3, 'steps: ')
字符串压缩,比如abbbc压缩为ab3c。
分析:如果字符是数字需要处理,比如aa2222b压缩为a224b,解压的时候就不知道是224个a,还是2个a,4个2或者其他。
解决办法,如果是数字,在前面加一个特殊字符标识,同时这个特殊字符也要单独处理
(前面加特殊字符是最精简的方法,只需要一个特殊字符,另外对于比较散乱的字符串,也就是单个字符很多的情况,会有很多1,所以如果字符数量是1则不压缩。)
'use strict';
const small = function (arr) {
let strList;
if (Array.isArray(arr)) strList = arr;
else if (typeof arr === 'string') strList = arr.split('');
else throw new Error('....');
let arrStr = '';
let count = 1;
for (let i = 0; i < strList.length; i++) {
if (strList[i + 1] === strList[i]) {
count++;
} else {
if (/\d/.test(strList[i]) || /'/.test(strList[i])) arrStr += `'${strList[i]}`;
else arrStr += `${strList[i]}`;
if (count !== 1) arrStr += `${count}`;
count = 1;
}
}
return arrStr;
};
const str = 'abgjldfff11111111111f4ous\'\'\'';
console.log(small(str));
快速排序
'use strict';
const quickSort = function (array, low, high) {
if (low >= high) {
return;
}
let i = low;
let j = high;
const tmp = array[i];
while (i < j) {
while (i < j && array[j] >= tmp) {
j--;
}
if (i < j) {
array[i++] = array[j];
}
while (i < j && array[i] <= tmp) {
i++;
}
if (i < j) {
array[j--] = array[i];
}
}
array[i] = tmp;
quickSort(array, low, i - 1);
quickSort(array, i + 1, high);
}
const arr = [1, 23, 7, 123, 45, 78, 10];
console.log(arr);
quickSort(arr, 0, arr.length - 1);
console.log(arr);
归并排序
'use strict';
// 两个有序数组合并
const memeryArray = function (arr1, arr2) {
const c = [];
let i = 0;
let j = 0;
let k = 0;
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) c[k++] = arr1[i++];
else c[k++] = arr2[j++];
}
while (i < arr1.length) c[k++] = arr1[i++];
while (j < arr2.length) c[k++] = arr2[j++];
return c;
}
// 将同一数组,两段有序序列合并
const memery = function (arr, first, mid, last) {
const temp = [];
let i = first;
const j = mid;
let m = mid + 1;
const n = last;
let k = 0;
while (i <= j && m <= n) {
if (arr[i] < arr [m]) temp[k++] = arr[i++];
else temp[k++] = arr[m++];
}
while (i <= j) temp[k++] = arr[i++];
while (m <= n) temp[k++] = arr[m++];
for (i = 0; i < k; i++) arr[first + i] = temp[i];
}
const mergeSort = function (arr, first, last) {
if (!Array.isArray(arr)) throw new Error('argument is not a array');
if (first < last) {
const mid = parseInt((first + last) / 2, 10);
mergeSort(arr, first, mid);
mergeSort(arr, mid + 1, last);
memery(arr, first, mid, last);
}
};
const arr1 = [1, 7, 13, 20, 6, 9, 10, 10, 11, 26, 29];
console.log(arr1);
mergeSort(arr1, 0, arr1.length - 1);
console.log(arr1);
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。