Skip to content

Commit

Permalink
feat: add Algo
Browse files Browse the repository at this point in the history
  • Loading branch information
WL2O2O committed Mar 10, 2024
1 parent 3a31c90 commit dda5d93
Show file tree
Hide file tree
Showing 30 changed files with 115 additions and 26 deletions.
7 changes: 7 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 更新Timeline_2024_03
| ID | TITLE | UPDATE_TIME | AUTHOR |
| ---- | --------------------------------------------- | ------------ | ----------------------------------- |
| 2 | [汉得24春招Java研发第一批](./bagu/Algo/汉得24春招第一批笔试.md) | 03-09 18:40 | [WL2O2O](https://github.com/WL2O2O) |
| 1 | [美团24春招软开(到店业务)](./bagu/Algo/美团24春招笔试.md) | 03-09 18:26 | [WL2O2O](https://github.com/WL2O2O) |
| 0 | [链表](./bagu/Algo/链表.md) | 03-02 14:36 | [WL2O2O](https://github.com/WL2O2O) |

## 更新Timeline_2024_01

| ID | TITLE | UPDATE_TIME | AUTHOR |
Expand Down
Binary file removed src/bagu/Algo/img/image.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image1.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image10.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image11.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image12.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image13.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image14.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image15.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image16.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image17.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image18.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image19.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image20.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image21.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image22.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image23.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image24.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image25.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image26.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image27.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image3.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image4.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image6.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image7.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image8.png
Binary file not shown.
Binary file removed src/bagu/Algo/img/image9.png
Diff not rendered.
25 changes: 25 additions & 0 deletions src/bagu/Algo/汉得24春招第一批笔试.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
> ## Voiceover:
>
> ***见者有缘,缘来好运。欢迎大家来到我的博客【CS_GUIDER】:
> [https://wlei224.gitee.io](https://wlei224.gitee.io/) (建议访问这个,速度极快)
> [https://wl2o2o.github.io](https://wl2o2o.github.io/) (建议收藏至浏览器书签)***
>
> 我的开源博客涵盖了**八股文****Java基础****JVM****MySQL****Linux****框架技术****算法**以及其他领域的文章,博客域名长期有效!!!如果本站对您来说有用,请收藏本文链接奥。万分感谢。请放心,开源博客,没有任何套路。
>
> 个人博客建站教程长期不定时连载,囊括我基于 Hexo | fluid 主题的搭建版本记录以及搭建踩坑记录,还有基于原 fluid 主题增加的小功能,如果感兴趣,欢迎大家在页脚评论区咨询。
>
> ![博客文章](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202311210931702.png)
#### 美团直接5道编程题,但是整体难度偏中等,汉得还有选择题可以蒙一下🤣,但是编程题直接来了两个二叉树,没刷过呀T_T,反正我是想直接交卷了··· ···

![alt text](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091838374.png)



#### 第一道“母夜叉”:

![alt text](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091838299.png)

#### 第二道“母夜叉”:

![alt text](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091839037.png)
52 changes: 26 additions & 26 deletions src/bagu/Algo/算法笔记.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public static int linearSearch(int[] a, int target){

时间复杂度不依赖于环境因素

![img](./img/image.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831464.png)

![img](./img/image1.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831465.png)

说明:渐进上界中的g(n)可以代表算法的最好情况 (大O表示法)

Expand All @@ -115,11 +115,11 @@ public static int linearSearch(int[] a, int target){

大O表示法的解析:

![img](./img/image3.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831466.png)

常见的大O表示法:

![img](./img/image4.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831468.png)

空间复杂度:

Expand Down Expand Up @@ -244,7 +244,7 @@ public static int binarySearchRightmost2(int[] a, int target){

查找最左、最右索引位置的应用:

![img](./img/image6.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831469.png)

排名:指查找的值target在这组数中排第几

Expand All @@ -266,9 +266,9 @@ public static int binarySearchRightmost2(int[] a, int target){

力扣题:

![img](./img/image7.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831470.png)

![img](./img/image8.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831471.png)

示例:

Expand All @@ -291,9 +291,9 @@ class Solution{
}
```

![img](./img/image9.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831473.png)

![img](./img/image10.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831474.png)

```java
class Solution{
Expand All @@ -316,9 +316,9 @@ class Solution{
}
```

![img](./img/image11.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831475.png)

![img](./img/image12.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831476.png)

```java
class Solution{
Expand Down Expand Up @@ -409,7 +409,7 @@ java中数组结构为

空间占用示意图:

![img](./img/image13.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831477.png)

随机访问

Expand Down Expand Up @@ -550,7 +550,7 @@ public class DynamicArray implements Interable<Integer>{

内存图如下:

![img](./img/image14.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831478.png)

- 二维数组占32个字节,其中array【0】,array【1】,array【2】三个元素分别保存了指向三个一维数组的引用
- 三个一维数组各占40个字节
Expand Down Expand Up @@ -581,9 +581,9 @@ public class DynamicArray implements Interable<Integer>{

分类:

![img](./img/image15.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831480.png)

![img](./img/image16.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831481.png)

```java
public class SinglyLinkedList{//整体
Expand Down Expand Up @@ -1039,7 +1039,7 @@ public class DoubleLinkedListSentinel implements Iterable<Integer>{

双向环形链表带哨兵,这时的哨兵既作为头,也作为尾。

![img](./img/image17.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831482.png)

```java
public class DoublyLinkedListSentinel implements Interable<Integer>{
Expand Down Expand Up @@ -1195,9 +1195,9 @@ void f(Node node = 1){

用递归方法的解题思路:

![img](./img/image18.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831483.png)

![img](./img/image19.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831484.png)

```java
public class Factorial{
Expand All @@ -1215,7 +1215,7 @@ public class Factorial{
}
```

![img](./img/image20.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831485.png)

```java
public class ReversePrintString{
Expand Down Expand Up @@ -1340,7 +1340,7 @@ public class E05InsertionSort{

single recursion 单路递归 multi recursion 多路递归

![img](./img/image21.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831486.png)

```java
public class E06Fibonacci{
Expand All @@ -1362,13 +1362,13 @@ public class E06Fibonacci{
}
```

![img](./img/image22.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831487.png)

![img](./img/image23.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831488.png)

兔子问题规律:f(n) = f(n-1)+f(n-2) n是从第一项(F1)开始

![img](./img/image24.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831489.png)

青蛙爬楼梯:n是从第二项(F2)开始,规律:f(n)=f(n-1)+f(n+1)

Expand Down Expand Up @@ -1487,8 +1487,8 @@ public static void main(String[] args){
}
```

![img](./img/image25.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831490.png)

![img](./img/image26.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831491.png)

![img](./img/image27.png)
![img](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091831493.png)
57 changes: 57 additions & 0 deletions src/bagu/Algo/美团24春招笔试.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
> ## Voiceover:
>
> ***见者有缘,缘来好运。欢迎大家来到我的博客【CS_GUIDER】:
> [https://wlei224.gitee.io](https://wlei224.gitee.io/) (建议访问这个,速度极快)
> [https://wl2o2o.github.io](https://wl2o2o.github.io/) (建议收藏至浏览器书签)***
>
> 我的开源博客涵盖了**八股文****Java基础****JVM****MySQL****Linux****框架技术****算法**以及其他领域的文章,博客域名长期有效!!!如果本站对您来说有用,请收藏本文链接奥。万分感谢。请放心,开源博客,没有任何套路。
>
> 个人博客建站教程长期不定时连载,囊括我基于 Hexo | fluid 主题的搭建版本记录以及搭建踩坑记录,还有基于原 fluid 主题增加的小功能,如果感兴趣,欢迎大家在页脚评论区咨询。
>
> ![博客文章](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202311210931702.png)
如图所示:
![笔试中的某一题](https://cs-wlei224.obs.cn-south-1.myhuaweicloud.com/blog-imgs/202403091401792.png)

我的题解:

```java
package leetcode;
import java.util.ArrayList;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long cnt = 0;
long cnt0 = 0;
ArrayList<Integer> list = new ArrayList<>();
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
int q = in.nextInt();
for (int i = 0; i < n; i++) {
list.add(in.nextInt());
cnt += list.get(i);
if (list.get(i) == 0) cnt0++;
// System.out.print(list.get(i));
}
for (int i = 0; i < q; i++) {
long l = in.nextLong();
long r = in.nextLong();
System.out.println((cnt + l*cnt0) + " " + (cnt + r*cnt0));
}
}
}
}
```


春招的第一个笔试,属实有点紧张了,没有想起来用前缀和数组,而是使用了最简单的且浪费效率遍历查询,最终虽然AC了90% ,但是还是超时了,造成没有全AC的原因之一可能还与没有处理[L,R]边界有关!

下面是来自AI的建议:
> 造成这段代码超时的原因在于:
> 重复计算:在处理每个查询时,代码都会遍历整个数组来重新计算总和cnt和0的个数cnt0。对于多个查询,这种做法会> 导致大量的重复计算。
> 无效操作:对每个查询范围内的元素并未进行任何针对性的计算,而是直接使用全局累计的cnt和cnt0进行求解。这种方> 式无法正确处理区间内包含0的情况,因为在计算结果时未剔除区间内的0的影响。
> 为了优化这个问题,应当在读取输入数组之后,预先计算出所有非零元素的累加和(可以采用前缀和的方法),同时记录> 每个位置之前连续非零元素的累加和。这样,在处理查询时,可以直接根据前缀和计算出给定区间的非零元素之和,从而> 避免重复计算并提高效率。

0 comments on commit dda5d93

Please sign in to comment.