Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
shfshanyue committed Apr 15, 2022
1 parent efc23e0 commit 7a050c3
Show file tree
Hide file tree
Showing 85 changed files with 8,377 additions and 7,479 deletions.
3 changes: 2 additions & 1 deletion .vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ module.exports = {
{ text: '开放式问题', link: '/open/' },
{ text: '大厂内推', link: '/infer/ali-ascp.md' },
{ text: '各地求职', link: '/job/chengdu.html' },
{ text: '面试路线图', link: '/roadmap/code.html' },
// { text: '面试路线图', link: '/roadmap/code.html' },
]
},
{ text: '面试路线图', link: '/roadmap/code.html' },
{ text: '面经大全', link: '/interview.html' },
{ text: 'Apifox', link: 'https://www.apifox.cn?utm_source=shanyue-question' },
// { text: '极客时间返现', link: 'https://geek.shanyue.tech' },
Expand Down
2 changes: 1 addition & 1 deletion .vuepress/theme/components/Bar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</div>
<div class="bar-intro">
<div class="text">
每天晚上九点 <a href="https://space.bilibili.com/28696526">B站讲解前端工程化直播</a>,并解答相关问题。
<a href="https://www.apifox.cn/?utm_source=shanyue-question">Apifox,在国内可替代 Postman 并强烈适用于团队的 API 管理工具。</a>
</div>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@

+ [前端大厂面试指南](https://codepen.io/collection/MggMed?grid_type=list)

## 特别赞助

[**Apifox: API 文档、API 调试、API Mock、API 自动化测试**](https://www.apifox.cn?utm_source=shanyue-question)

## 大厂内推

添加微信 `shanyue94`,免费大厂内推。
Expand Down
28 changes: 28 additions & 0 deletions base/algorithm/177.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,31 @@ description: "【Q176】如何在数组中找出三个数之和为N 字节跳动
::: tip Issue
欢迎在 Gtihub Issue 中回答此问题: [Issue 177](https://github.com/shfshanyue/Daily-Question/issues/177)
:::

::: tip Author
回答者: [HNHED](https://github.com/HNHED)
:::

排序之后使用双指针
let ar = [5, 12, 6, 3, 9, 2, 1, 7];

function getthreenum(arr, target, result = []) {
arr = arr.sort((a, b) => a - b)
const len = arr.length;
for (let i = 0; i < len; i++) {
let j = i + 1;
let k = len - 1;
while (j < k) {
if (arr[j] + arr[k] > target - arr[i]) {
k--;
} else if (arr[j] + arr[k] < target - arr[i]) {
j++;
} else {
result.push([arr[i], arr[j], arr[k]]);
j++;
}
}
}
return result;
}
console.log(getthreenum(ar, 13, []));
166 changes: 166 additions & 0 deletions base/algorithm/419.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,169 @@ const encode = (str) => {
return encodedArray.join("");
};
```
::: tip Author
回答者: [3N26](https://github.com/3N26)
:::
```javascript
function solution(s, limit) {
const n = s.length;
let res = "";
for (let i = 0, cnt = 0; i < n; i += cnt) {
cnt = 1;
while (s[i] === s[i + cnt]) cnt++;
res += cnt > limit ? s[i] + cnt : s[i].repeat(cnt);
}
return res;
}
```
::: tip Author
回答者: [LiJinWD](https://github.com/LiJinWD)
:::
const encode = word => {
if(!word) return "";
let ary = word.split('');
let group = {}
let result = ""
group[ary[0]] = 1
for(let i = 1, j = ary.length; i <= j; i++) {
if(ary[i - 1] != ary[i]) {
result += Object.entries(group).flat().join('')
group = {}
group[ary[i]] = 1
} else {
group[ary[i]]++
}
}
return result
}
const encode1 = word => {
return word.replace(/1/gi, '')
}
const encode2 = word => {
let one = word.substring(0, 1)
let newWord = ''
for(item of word) {
newWord += item == 2 ? one : item
one = item
}
return newWord
}
::: tip Author
回答者: [yuli-lovely](https://github.com/yuli-lovely)
:::
```js
function encode(str) {
let prefix = ""; //初识节点
let num = 0; //计数器
let result = ""; //结果
for (let i = 0; i < str.length; i++) {
if (i == 0) {
prefix = str[i];
}

if (prefix != str[i] || i == str.length - 1) {
if (i == str.length - 1) {
num++;
}
if (num == 1 || num == 2) {
result = result + prefix.repeat(num);
} else {
result = result + prefix + num;
}

prefix = str[i];
num = 0;
}
num++;
}
return result;
}
```
::: tip Author
回答者: [yuli-lovely](https://github.com/yuli-lovely)
:::
```js
// number<10--适用下面
function decode(str) {
let result = "";
for (let i = 1; i <= str.length; i++) {
console.log();
if (typeof parseInt(str[i]) === "number") {
result = result + str[i - 1].repeat(parseInt(str[i]));
}
}
return result;
}
//全场景适用
function decode2(str) {
let datas = Array.from(str.matchAll(/[a-z][0-9]*/g));
let result = "";
for (elem of datas) {
elem = elem[0];
result = result + elem[0];
if (elem.length > 1) {
result = result + elem[0].repeat(parseInt(elem.substr(1)) - 1);
}
}
return result;
}
```
::: tip Author
回答者: [JiangHuanLH](https://github.com/JiangHuanLH)
:::
好像没看到用正则的解法,我来补充一下:kissing:
感觉用正则来实现,修改编码条件也挺简单的
```js
function encode(str) {
let res = "";
const reg = /(\w)\1*/g;
const matchs = str.match(reg);
matchs.forEach((item) => {
if (item.length > 1) {
res += item[0] + item.length;
} else {
res += item[0];
}
});

return res;
}
```
::: tip Author
回答者: [fanfankill](https://github.com/fanfankill)
:::
```js
function encode(str) {
//
let index = 0;
let result = "";
while (index < str.length) {
let count = 1;
result += str[index];
while (str[index] == str[index + 1]) {
index++;
count++;
}
index++;
result += count;
}
console.log(result);
return result;
}
```
96 changes: 96 additions & 0 deletions base/algorithm/49.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,99 @@ this.items = [];
return result;
}
}

::: tip Author
回答者: [someGenki](https://github.com/someGenki)
:::

基于最大堆实现优先队列

```js
class MaxHeap {
constructor(arr = []) {
this.heap = []; // 用数组表示堆结构
arr.forEach((item) => this.add(item));
}

add(value) {
// O(logK) 插入节点值: 放入数组末尾并上浮到合适位置
this.heap.push(value);
this.shiftUp(this.heap.length - 1);
}

pop() {
// O(logK) 提取最大值/堆顶: 提取 heap[0] 并用 heap[-1] 进行代替,然后从顶部开始下沉到合适位置
const max = this.heap[0];
this.swap(0, this.size() - 1);
this.heap.pop();
this.shiftDown(0);
return max;
}

peek() {
// 获取最值/堆顶
return this.heap[0];
}

size() {
// 获取当前堆大小
return this.heap.length;
}

// ↓私有属性↓
swap(index1, index2) {
// 交换节点位置
const temp = this.heap[index1];
this.heap[index1] = this.heap[index2];
this.heap[index2] = temp;
}

parentIndex(index) {
// 获取父节点的位置 (index - 1) / 2 向下取整
return (index - 1) >> 1;
}

leftChildIndex(index) {
// 获取左子节点
return index * 2 + 1;
}

rightChildIndex(index) {
// 获取右子节点
return index * 2 + 2;
}

shiftUp(index) {
// 上浮节点,当前值小于父节点值时停止,使当前堆保持最大堆的性质
let parentIndex = this.parentIndex(index);
while (index > 0 && this.heap[parentIndex] < this.heap[index]) {
this.swap(index, parentIndex);
parentIndex = this.parentIndex((index = parentIndex));
}
}

shiftDown(index) {
// 下沉节点,当前值大于子节点值时停止,使当前堆保持最大堆的性质
const leftIndex = this.leftChildIndex(index);
const rightIndex = this.rightChildIndex(index);
// 先比较左子节点值,当前值小于左子节点,则交换,并递归进行下沉
if (this.heap[index] < this.heap[leftIndex]) {
this.swap(leftIndex, index);
this.shiftDown(leftIndex);
}
if (this.heap[index] < this.heap[rightIndex]) {
this.swap(rightIndex, index);
this.shiftDown(rightIndex);
}
}
}

// ==TEST==
const priorityQueue = new MaxHeap([2, 5, 3]);
console.log(priorityQueue.peek()); // 5
priorityQueue.add(7);
console.log(priorityQueue.peek()); // 7
priorityQueue.pop();
priorityQueue.add(1);
console.log(priorityQueue.peek()); // 5
```
18 changes: 18 additions & 0 deletions base/algorithm/701.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,21 @@ description: "【Q682】100层楼,两个玻璃球,求最少多少次测出
最后,让我们把第一个鸡蛋没碎的情况下,所尝试的楼层数完整列举出来:

14,27, 39, 50, 60, 69, 77, 84, 90, 95, 99, 100

::: tip Author
回答者: [3N26](https://github.com/3N26)
:::

```javascript
function solution(n) {
const dp = Array.from({ length: 2 }, () => new Array(n + 1).fill(0));
for (let i = 1; i <= n; i++) {
dp[0][i] = i;
dp[1][i] = n;
for (let j = 1; j <= i; j++) {
dp[1][i] = Math.min(dp[1][i], Math.max(dp[0][j - 1], dp[1][i - j]) + 1);
}
}
return dp[1][n - 1];
}
```
14 changes: 14 additions & 0 deletions base/algorithm/711.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,17 @@ function rand7() {
}
}
```

::: tip Author
回答者: [3N26](https://github.com/3N26)
:::

```javascript
function rand5() {
return (Math.random() * 6) | 0;
}

function rand7() {
return (rand5() & 1) * 4 + (rand5() & 1) * 2 + (rand5() & 1);
}
```
Loading

0 comments on commit 7a050c3

Please sign in to comment.