leetcode-master/problems/数组理论基础.md

64 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p align='center'>
<img src="https://img-blog.csdnimg.cn/20201215214102642.png" width=400 >
</p>
<p align="center">
<a href="https://github.com/youngyangyang04/leetcode-master"><img src="https://img.shields.io/badge/Github-leetcode--master-lightgrey" alt=""></a>
<a href="https://img-blog.csdnimg.cn/20201115103410182.png"><img src="https://img.shields.io/badge/刷题-微信群-green" alt=""></a>
<a href="https://img-blog.csdnimg.cn/20201210231711160.png"><img src="https://img.shields.io/badge/公众号-代码随想录-brightgreen" alt=""></a>
<a href="https://space.bilibili.com/525438321"><img src="https://img.shields.io/badge/B站-代码随想录-orange" alt=""></a>
<a href="https://www.zhihu.com/people/sun-xiu-yang-64"><img src="https://img.shields.io/badge/知乎-代码随想录-blue" alt=""></a>
<a href="https://www.toutiao.com/c/user/60356270818/#mid=1633692776932365"><img src="https://img.shields.io/badge/头条-代码随想录-red" alt=""></a>
</p>
# 数组理论基础
数组是非常基础的数据结构,在面试中,考察数组的题目一般在思维上都不难,主要是考察对代码的掌控能力
也就是说,想法很简单,但实现起来 可能就不是那么回事了。
首先要知道数组在内存中的存储方式,这样才能真正理解数组相关的面试题
**数组是存放在连续内存空间上的相同类型数据的集合。**
数组可以方便的通过下标索引的方式获取到下标下对应的数据。
举一个字符数组的例子,如图所示:
![算法通关数组](https://img-blog.csdnimg.cn/2020121411152849.png)
需要两点注意的是
* **数组下标都是从0开始的。**
* **数组内存空间的地址是连续的**
正是**因为数组的在内存空间的地址是连续的,所以我们在删除或者增添元素的时候,就难免要移动其他元素的地址。**
例如删除下标为3的元素需要对下标为3的元素后面的所有元素都要做移动操作如图所示
![算法通关数组1](https://img-blog.csdnimg.cn/2020121411155232.png)
而且大家如果使用C++的话要注意vector 和 array的区别vector的底层实现是array严格来讲vector是容器不是数组。
**数组的元素是不能删的,只能覆盖。**
那么二维数组直接上图,大家应该就知道怎么回事了
![算法通关数组2](https://img-blog.csdnimg.cn/20201214111612863.png)
**那么二维数组在内存的空间地址是连续的么?**
我们来举一个例子,例如: `int[][] rating = new int[3][4];` 这个二维数据在内存空间可不是一个 `3*4` 的连续地址空间
看了下图,就应该明白了:
![算法通关数组3](https://img-blog.csdnimg.cn/20201214111631844.png)
所以**二维数据在内存中不是 `3*4` 的连续地址空间,而是四条连续的地址空间组成!**
很多同学会以为二维数组在内存中是一片连续的地址,其实并不是。
这里面试中数组相关的理论知识就介绍完了。
后续我将介绍面试中数组相关的五道经典面试题目,敬请期待!