一些算法题

统计字符串中每个字符出现的次数

reduce()函数适合作为累加器,将initialValue设置为一个空对象,initialValue作为累加器accumulator的初始值,依次往后执行每个元素。如果执行的元素在accumulator中存在,则将其计数加1,否则将当前执行元素作为accumulator的key,其value为1。依次执行完所有元素后,最终返回的accumulator的值就包含了每个元素出现的次数。

1
2
3
4
5
6
var countOccurrences = (arr) => arr.reduce((accumulator, currentValue) => {
accumulator[currentValue] ? accumulator[currentValue]++ :
accumulator[currentValue] = 1;
return accumulator;
}, {});
countOccurrences([1, 2, 3, 2, 2, 5, 1]); // {1: 2, 2: 3, 3: 1, 5: 1}

统计字符串中出现次数最多的字符及出现的次数

假如存在一个字符串’helloJavascripthellohtmlhellocss’,其中出现次数最多的字符是l,出现的次数是7次。

通过key-value形式的对象来存储字符串以及字符串出现的次数,然后逐个判断出现次数最大值,同时获取对应的字符

这个几乎最简单的也是最容易想到的解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function getMaxCount(str) {
var json = {};
// 遍历str的每一个字符得到key-value形式的对象
for (var i = 0; i < str.length; i++) {
// 判断json中是否有当前str的值
if (!json[str.charAt(i)]) {
// 如果不存在,就将当前值添加到json中去
json[str.charAt(i)] = 1;
} else {
// 如果存在,则让value值加1
json[str.charAt(i)]++;
}
}
// 存储出现次数最多的值和出现次数
var maxCountChar = '';
var maxCount = 0;
// 遍历json对象,找出出现次数最大的值
for (var key in json) {
// 如果当前项大于下一项
if (json[key] > maxCount) {
// 就让当前值更改为出现最多次数的值
maxCount = json[key];
maxCountChar = key;
}
}
//最终返回出现最多的值以及出现次数
return '出现最多的值是' + maxCountChar + ',出现次数为' + maxCount;
}
var str = 'helloJavaScripthellohtmlhellocss';
getMaxCount(str); // '出现最多的值是l,出现次数为7'

去除字符串中重复的字符

ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值。可以利用这个特性去重。

1
2
3
4
5
6
7
var str = 'helloJavaScripthellohtmlhellocss';
function removeDuplicateChar(str) {
const arr = Array.prototype.slice.call(str)
const res = Array.from(new Set([...arr])).join('')
console.log(res); // heloJavScriptms
}
removeDuplicateChar(str)

使用key-value类型的对象存储,key表示唯一的字符,处理完后将所有的key拼接在一起即可得到去重后的结果

1
2
3
4
5
6
7
8
9
10
11
12
function removeDuplicateChar(str) {
const res = []
const json = {}
for (let i = 0; i < str.length; i++) {
const char = str[i]
if (!json[char]) {
json[char] = true
res.push(char)
}
}
console.log(res.join(''));
}

主要思想是借助数组的filter()函数,然后在filter()函数中使用indexOf()函数判断

1
2
3
4
5
function removeDuplicateChar(str) {
const arr = Array.prototype.slice.call(str)
const res = arr.filter((item, index) => arr.indexOf(item) === index)
console.log(res);
}

判断一个字符串是否为回文字符串

回文字符串是指一个字符串正序和倒序是相同的,例如字符串’abcdcba’是一个回文字符串,而字符串’abcedba’则不是一个回文字符串。

最简答的思路,将字符串翻转,然后与原来的字符串进行比较

1
2
3
4
5
6
7
8
9
function isPalindromicStr(str) {
const str1 = Array.prototype.slice.call(str).reverse().join('')
console.log(str === str1);
}

var str1 = 'abcdcba';
var str2 = 'abcedba';
isPalindromicStr(str1); // true
isPalindromicStr(str2); // false

将字符串按从前往后顺序的字符与按从后往前顺序的字符逐个进行比较,如果遇到不一样的值则直接返回“false”,否则返回“true”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function isPalindromicStr(str) {
if (str.length === 0) {
return true
}
str = str.toLowerCase().split('')
let start = 0, end = str.length - 1
while (start < end) {
if (str[start] === str[end]) {
start++
end--
} else {
return false
}
}
return true
}

求数组中的最大值和最小值

最简单的方式当然是:

1
2
const values = [1, 2, 5, 7, 1, 6, 8];
console.log(Math.min(...values));; // 1

问题肯定是不止一种解决方案的,今天我们来用reduce()解决,主要思想是reduce()函数不设置initialValue初始值,将数组的第一个元素直接作为回调函数的第一个参数,依次与后面的值进行比较。当需要找最大值时,每轮累加器返回当前比较中大的值;当需要找最小值时,每轮累加器返回当前比较中小的值。

1
const Max = (arr) => arr.reduce((preValue, curValue) => preValue > curValue ? preValue : curValue)

一些算法题
https://xypecho.github.io/2022/11/22/一些算法题/
作者
很青的青蛙
发布于
2022年11月22日
许可协议