整数反转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* @description: 递归实现整数反转
* @param {一个整数}
* @return: 反转后的字符串,有需要的话可以用Number.parseInt()转换成Number
*/
const reverse = (num) => {
function reverseRecursion(num ,res = '') {
if (num > 10) {
let tmp = num % 10
res = res.concat(tmp)
return reverseRecursion(Math.floor(num / 10), res)
} else {
res = res.concat(num);
return res
}
}
return reverseRecursion(num)
}
reverse(1234)
链表反转
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
/**
* @description: 反转链表
* @param {头结点}
* @return: 反转后的链表的头结点
* 表结点数据结构如下
*/
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function ReverseList(pHead)
{
// write code here
let middle = pHead
if (middle === null || middle.next === null) return middle;
let pre = null
// pre.next = null
while(1){
if(middle.next === null) {
middle.next = pre
return middle
}
let nextNode = middle.next
middle.next = pre
pre = middle
middle = nextNode
// next = next.next
}
return middle
}
CSS
属性格式转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @description:
css 中经常有类似 background-image 这种通过 - 连接的字符,通过 javascript 设置样式的时候需要将这种样式转换成 backgroundImage 驼峰格式,请完成此转换功能。
输入:-webkit-background-image
输出:webkitBackgroundImage
* @param {属性名}
* @return: 转换后的属性名
*/
const convertToCamelCase = attr => {
let ary = attr.split('-');
ary.shift();
for (let index = 0; index < ary.length; index++) {
if (index !== 0) {
ary[index] = ary[index][0].toUpperCase() + ary[index].slice(1);
}
}
return ary.join('');
};
convertToCamelCase('-webkit-background-image');
出现最多的字符
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
/**
* @description:
请判断一个字符串出现次数最多的字符及出现次数?
输入:const str = 'asddfgdfwwqeweqwezczxcsdfgdgd';
输出:’d’, 出现6次
* @param {字符串}
* @return:
*/
const maxCountChar = (str) => {
let map = new Map()
for (const i of str) {
if (map.has(i)) {
map.set(i, (map.get(i)) + 1)
} else {
map.set(i, 1)
}
}
// return Math.max(...map.values())
let max = [null, 0]
for (const i of map.entries()) {
if (i[1] > max[1]) {
max = i
}
}
return max
}
maxCountChar('asddfgdfwwqeweqwezczxcsdfgdgd')
ASCII码排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @description:
* 题目描述
求字符串 'hello world' 对应的ASCII码数组,并按照编码大小逆序。
输入:'hello world’
输出:[119, 114, 111, 111, 108, 108, 108, 104, 101, 100, 32]
* @param {字符串}
* @return: ASCII组成的逆序数组
*/
const ascii = (str) => {
let tmpAry = str.split(''), result = []
result = tmpAry.map((el, index) => {
return el.charCodeAt()
})
console.log(result);
result.sort((a, b) => b - a)
return result
}
ascii('hello world')
丑数
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
31
32
33
34
35
36
/**
* @description: 丑数,题目:我们把只含有因子2、3、5的数称为丑数。例如6、8都是丑数,而14不是丑数,因为它含有因子7.通常也把1当做丑数。编程找出1500以内的全部丑数。注意:使用的算法效率应尽量高。
* @param {范围 / 丑数的个数}
* @return: {丑数组成的数组 / 第n个丑数}
* @logic: 设定初始的丑数为1,以1为起始,进行`*2,*3,*5`的操作,并取得tmp2,tmp3,tmp5这几个值的最小值,确认其不在已知的丑数数组中,再将其添加进数组。
* init: result = [1],
* then: tmp2 = 1 * 2, tmp3 = 1 * 3, tmp5 = 1 * 5,
* push(min(tmp2, tmp3, tmp5)), result = [1, 2],
* next: tmp2 = 2 * 2, tmp3 = 1 * 3, tmp5 = 1 * 5, result = [1, 2, 3]
* ...
*/
const getUglyNumber = n => {
if (n >= 1) {
const temp = [1],
result = [1];
let i = 1,
index2 = 0,
index3 = 0,
index5 = 0;
while (result.length < n) {
temp[i] = Math.min(temp[index2] * 2, temp[index3] * 3, temp[index5] * 5);
if (temp[i] == temp[index2] * 2) {
index2++;
} else if (temp[i] == temp[index3] * 3) {
index3++;
} else if (temp[i] == temp[index5] * 5) {
index5++;
}
if (result.indexOf(temp[i]) == -1) {
result.push(temp[i]);
}
i++;
}
return result[n - 1];
}
};