文章目录
- 数据结构
- 数组(顺序表)
- 特点
- 使用Java实现更高级的数组
- C语言实现
- 总结
- 优点
- 缺点
- 例题
- [26. 删除有序数组中的重复项](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/)
- [1. 两数之和](https://leetcode.cn/problems/two-sum/)
- [27. 移除元素](https://leetcode.cn/problems/remove-element/)
- [153. 寻找旋转排序数组中的最小值](https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/)
- [485. 最大连续 1 的个数](https://leetcode.cn/problems/max-consecutive-ones/)
- [414. 第三大的数](https://leetcode.cn/problems/third-maximum-number/)
- [2656. K 个元素的最大和](https://leetcode.cn/problems/maximum-sum-with-exactly-k-elements/)
- [LCP 06. 拿硬币](https://leetcode.cn/problems/na-ying-bi/)
- [2057. 值相等的最小索引](https://leetcode.cn/problems/smallest-index-with-equal-value/)
- [26. 删除有序数组中的重复项](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/)
- [2125. 银行中的激光束数量](https://leetcode.cn/problems/number-of-laser-beams-in-a-bank/)
- [27. 移除元素](https://leetcode.cn/problems/remove-element/)
- [1431. 拥有最多糖果的孩子]
数据结构
数组(顺序表)
- 一组相同数据类型的集合
特点
- 数组在内存中是连续分配的 如上图
- 创建时要指明数组的大小
数组名
代表首地址,索引从0
开始,到数组的长度-1
- 数组一旦创建好,大小不可以改变
- 使用索引
- 获取索引位置的值
arr[index]
- 修改
arr[index] = val
- 删除 (假删除)
就是把目标元素后面的元素向前位移一格
- 遍历,将数组中的元素,依次打印出来
- 获取索引位置的值
使用Java实现更高级的数组
import java.util.Random;public class MyArr<T> {private int capacity = 0;private int size = 0;private T[] arr;public MyArr(int capacity) {if (capacity < 0) this.capacity = 10; //if no right input, we will initial capacity 10this.capacity = capacity;this.arr = (T[]) new Object[capacity];}public int getCapacity() {return capacity;}public int getSize() {return size;}public T[] setCapacity(int capacity) {if (capacity < 0) {throw new RuntimeException("扩大小异常");}this.capacity = capacity;T[] newNum = (T[]) new Object[capacity];for (int i = 0; i < this.size; ++i) {newNum[i] = this.arr[i];}return newNum;}//增加元素public void add(T val) {if (this.size >= this.capacity) {this.arr = setCapacity(2 * this.capacity);}this.arr[this.size++] = val;}//删除元素public boolean removeByIndex(int index) {if (index < 0 || index > this.capacity) {throw new RuntimeException("数组越界");}for (int i = index; i < size - 1; ++i) {arr[i] = arr[i + 1];}size--;if (size < this.capacity / 4 && this.capacity > 4) {arr = setCapacity(this.capacity / 4);}return true;}//修改位置元素public void modify(int index, T val) {if (index < 0 || index > size - 1) {throw new RuntimeException("数组越界");}arr[index] = val;}//获取某元素位置public int locateVal(T val) {for (int i = 0; i < size; ++i) {if (arr[i] == val) {return i;//return index}}// if no find return -1return -1;}//打印元素@Overridepublic String toString() {StringBuffer stringBuffer = new StringBuffer();stringBuffer.append('[');for (int i = 0; i < this.size - 1; ++i) {stringBuffer.append(arr[i] + ",");}if(size>0) stringBuffer.append(arr[size - 1]);stringBuffer.append(']');return stringBuffer.toString();}}
C语言实现
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <stdlib.h> // 添加头文件以使用malloc和free函数
#define ERROR 0
#define OK 1
#define MAXLENGTH 20
#define List_Increment 10typedef int Status;//判断程序是否运行成功 0-失败 1-成功
typedef int Element;
typedef struct {Element* location;int length;//存储数据结构数组长度int listsize;//
}SqList;//初始化顺序表
Status initiElem(SqList *L) {//L->location = Element stu[20];L->location = (Element*)malloc(MAXLENGTH*sizeof(Element));L->length = 0 ;L->listsize = 0;if (L->location == NULL) {return ERROR ;//申请内存失败}return OK;//申请内存成功}
//插入元素
Status insertElem(SqList* L, int i, Element e) {if (L->length == MAXLENGTH) return ERROR;//表饱和if (i<1 || i>L->length + 1) return ERROR;if (i <= L->length) {for (int j = L->length - 1; j >= i - 1; j--) {L->location[j + 1] = L->location[j + 1];}L->location[i-1] = e;L->length++;//长度+1}//若i在末尾直接插入L->location [i - 1] = e;L->length++;//长度+1return OK;
}//查找元素
int findElem(SqList L, Element e) {for (int i = 0; i < L.length; i++) {if (L.location[i] == e) {return i + 1;//返回第i个元素}}return ERROR;}
//获取元素
Status getElem(SqList L, int i, Element* e) {if (i < 1 || i>L.length )return ERROR;e = L.location[i];//把第i各元素返回给Element类型数据return OK;
}
//删除操作
Status deleElem(SqList* L, int i) {if (L->length == 0)return ERROR;//此时为空表if (i<1 || i>L->length) return ERROR;//位置不合理//删除后面元素前移for (int j = i; j < L->length;j++) {L->location[j - 1] = L->location[j];}L->length--;return OK;//删除成功
}int main1() {SqList L;Element element=0;int choice;int index;//插入位置while (OK) {printf("---欢迎使用链表查询系统-----\n");printf("--------1-初始化链表--------\n");printf("--------2-插入链表元素------\n");printf("--------3-查找元素----------\n");printf("--------4-删除元素----------\n");printf("--------5-获取元素----------\n");printf("--------6-遍历元素----------\n");printf("--------7-退出系统----------\n");scanf("%1d", &choice);switch (choice){case 1://调用initiElem函数开始初始化if (initiElem(&L) == NULL) {printf("初始化失败!\n");return ERROR;}printf("初始化成功!\n");break;case 2:printf("请输入你想插入的元素(整数):\n");scanf("%d", &element);printf("请输入你想插入的位置:\n");scanf("%d", &index);if (insertElem(&L, index, element) == 0) {printf("插入失败!\n");break;}printf("插入成功!\n");break;case 3:printf("请输入你想查找的元素(整数):\n");scanf("%d", &element);if (findElem(L,element) == 0) {printf("查找失败\n");break;}printf("该元素在第%d个位置\n", findElem(L,element));break;case 4:printf("请输入你想删除的元素(位置):\n");scanf("%d", &index);if (deleElem(&L,index) == 0) {printf("删除失败!\n");break;}printf("删除成功!\n");break;case 5:printf("请输入你想获取元素的位置:\n");scanf("%d", &index);if (getElem(L, index, &element) == 0) {printf("获取失败!");break;}printf("第%d的元素为 %d", index + 1, element);break;case 6:if (L.length == 0) {printf("为空表,无法遍历");break;}for (int i = 0; i < L.length; i++) {printf("%d\n", *(L.location+i));}break;default:break;}}
}
总结
优点
1.由于是根据下表查找数据,则数组(顺序表)查找速度快
时间复杂度O(1)
缺点
1.因为每次删除元素,或者插入数组时,每个数据都需要向后或者向前位移则平均时间复杂度为O(n)
,所以插入或删除速度慢