Skip to content

Commit

Permalink
feat: 每日一题算法+面试
Browse files Browse the repository at this point in the history
  • Loading branch information
joywins-y committed Oct 26, 2023
1 parent 15725e3 commit 7c50317
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/.vitepress/sidebar/algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export default [
text: "1726. 同积元组",
link: "/algorithm/1726.tuple-with-same-product",
},
{
text: "2520. 统计能整除数字的位数",
link: "/algorithm/2520.count-the-digits-that-divide-a-number",
},
{
text: "2525. 根据规则将箱子分类",
link: "/algorithm/2525.categorize-box-according-to-criteria",
Expand All @@ -30,6 +34,10 @@ export default [
text: "2678. 老人的数目",
link: "/algorithm/2678.number-of-senior-citizens",
},
{
text: "2698. 求一个整数的惩罚数",
link: "/algorithm/2698.find-the-punishment-number-of-an-integer",
},
],
},
];
8 changes: 8 additions & 0 deletions docs/.vitepress/sidebar/interview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export default [
text: "23-10-24",
link: "/interview/daily-question/23-10-24",
},
{
text: "23-10-25",
link: "/interview/daily-question/23-10-25",
},
{
text: "23-10-26",
link: "/interview/daily-question/23-10-26",
},
],
},
{
Expand Down
77 changes: 77 additions & 0 deletions docs/algorithm/2520.count-the-digits-that-divide-a-number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 2520. 统计能整除数字的位数

> 难度:<span style="color: #00af9b; font-weight: 500">简单</span>
>
> 地址:https://leetcode.cn/problems/count-the-digits-that-divide-a-number/description/
给你一个整数 `num` ,返回 `num` 中能整除 `num` 的数位的数目。

如果满足 `nums % val == 0` ,则认为整数 `val` 可以整除 `nums`

**示例 1:**

> **<font color=#000>输入</font>**:num = 7
>
> **<font color=#000>输出</font>**:1
>
> **<font color=#000>解释</font>**:7 被自己整除,因此答案是 1 。
**示例 2:**

> **<font color=#000>输入</font>**:num = 121
>
> **<font color=#000>输出</font>**:2
>
> **<font color=#000>解释</font>**:121 可以被 1 整除,但无法被 2 整除。由于 1 出现两次,所以返回 2 。
**示例 3:**

> **<font color=#000>输入</font>**:num = 1248
>
> **<font color=#000>输出</font>**:4
>
> **<font color=#000>解释</font>**:1248 可以被它每一位上的数字整除,因此答案是 4 。
**提示:**

- `1 <= num <= 10^9`
- `num` 的数位中不含 `0`

**题解:**

```js
/**
* @param {number} num
* @return {number}
*/
var countDigits = function (num) {
const str = num.toString();
let ans = 0;
for (let i = 0; i < str.length; i++) {
if (num % str[i] == 0) {
ans += 1;
}
}
return ans;
};
```

或者

```js
/**
* @param {number} num
* @return {number}
*/
var countDigits = function (num) {
let t = num;
let ans = 0;
while (t) {
if (num % (t % 10) == 0) {
ans += 1;
}
t = Math.floor(t / 10);
}
return ans;
};
```
4 changes: 4 additions & 0 deletions docs/algorithm/2525.categorize-box-according-to-criteria.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
**示例 1:**

> **<font color=#000>输入</font>**:length = 1000, width = 35, height = 700, mass = 300
>
> **<font color=#000>输出</font>**:"Heavy"
>
> **<font color=#000>解释</font>**
> 箱子没有任何维度大于等于 10^4^ 。
> 体积为 24500000 <= 10^9^ 。所以不能归类为 "Bulky" 。
Expand All @@ -29,7 +31,9 @@
**示例 2:**

> **<font color=#000>输入</font>**:length = 200, width = 50, height = 800, mass = 50
>
> **<font color=#000>输出</font>**:"Neither"
>
> **<font color=#000>解释</font>**
> 箱子没有任何维度大于等于 10^4^ 。
> 体积为 8 \* 10^6^ <= 10^9^ 。所以不能归类为 "Bulky" 。
Expand Down
78 changes: 78 additions & 0 deletions docs/algorithm/2698.find-the-punishment-number-of-an-integer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 2698. 求一个整数的惩罚数

> 难度:<span style="color: #ffb800; font-weight: 500">中等</span>
>
> 地址:https://leetcode.cn/problems/find-the-punishment-number-of-an-integer/
给你一个正整数 `n` ,请你返回 `n`**惩罚数**

`n`**惩罚数** 定义为所有满足以下条件 `i` 的数的平方和:

- `1 <= i <= n`
- `i * i` 的十进制表示的字符串可以分割成若干连续子字符串,且这些子字符串对应的整数值之和等于 i 。

**示例 1:**

> **<font color=#000>输入</font>**:n = 10
>
> **<font color=#000>输出</font>**:182
>
> **<font color=#000>解释</font>**:总共有 3 个整数 i 满足要求:
> \- 1 ,因为 1 \* 1 = 1
> \- 9 ,因为 9 \* 9 = 81 ,且 81 可以分割成 8 + 1 。
> \- 10 ,因为 10 \* 10 = 100 ,且 100 可以分割成 10 + 0 。
> 因此,10 的惩罚数为 1 + 81 + 100 = 182
**示例 2:**

> **<font color=#000>输入</font>**:n = 37
>
> **<font color=#000>输出</font>**:1478
>
> **<font color=#000>解释</font>**:总共有 4 个整数 i 满足要求:
> \- 1 ,因为 1 \* 1 = 1
> \- 9 ,因为 9 \* 9 = 81 ,且 81 可以分割成 8 + 1 。
> \- 10 ,因为 10 \* 10 = 100 ,且 100 可以分割成 10 + 0 。
> \- 36 ,因为 36 \* 36 = 1296 ,且 1296 可以分割成 1 + 29 + 6 。
> 因此,37 的惩罚数为 1 + 81 + 100 + 1296 = 1478
**提示:**

- `1 <= n <= 1000`

**题解:**

![idea](../public/algorithm/idea.png)

```js
/**
* @param {number} n
* @return {number}
*/
var punishmentNumber = function (n) {
const dfs = (s, pos, tot, target) => {
if (pos == s.length) {
return tot == target;
}
let sum = 0;
for (let i = pos; i < s.length; i++) {
sum = sum * 10 + parseInt(s[i]);
if (tot + sum > target) {
break;
}
if (dfs(s, i + 1, tot + sum, target)) {
return true;
}
}
return false;
};
let ans = 0;
for (let i = 1; i <= n; i++) {
let s = (i * i).toString();
if (dfs(s, 0, 0, i)) {
ans += i * i;
}
}
return ans;
};
```
7 changes: 7 additions & 0 deletions docs/interview/daily-question/23-10.25.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 【头条】React Hooks 如何替代或部分替代 redux 功能

我们把全局 store 分为两块

1. 从服务器端来,如各种 `model`,此时可以使用 `swr` 直接替代。或者封装一个 `useModel`,如 `useUser``usePermission`

2. 客户端全局 store,此时可以使用 `useReducer` 和 useCo`ntext 来替代。
7 changes: 7 additions & 0 deletions docs/interview/daily-question/23-10.26.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 【阿里】fetch 中 credentials 指什么意思,可以取什么值

`credentials` 指在使用 `fetch` 发送请求时是否应当发送 `cookie`

- `omit`:从不发送 `cookie`
- `same-origin`:同源时发送 `cookie`(浏览器默认值)
- `include`:同源与跨域都发送 `cookie`
Binary file added docs/public/algorithm/idea.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7c50317

Please sign in to comment.