当前位置: 首页 > news >正文

【算法集训】基础数据结构:一、顺序表(上)

顺序表是最基础的数组结构,所有数据都按顺序存储。

第一题 1464. 数组中两元素的最大乘积

https://leetcode.cn/problems/maximum-product-of-two-elements-in-an-array/description/
第一种:常规解法,遍历两次数组根据条件比较出最大的即可

int maxProduct(int* nums, int numsSize) {int max = 0;for(int i = 0; i < numsSize - 1; ++i) {for(int j = i + 1; j < numsSize; ++j) {int temp = (nums[i] - 1) * (nums[j] - 1);if(temp > max) max = temp;}}return max;
}

第二种:使用排序,先排序数组,然后直接将最大的和次大的做运算得出结果。

int cmp(const void * p1, const void *p2) {return (*(int *) p1) - (* (int *) p2);
}int maxProduct(int* nums, int numsSize) {qsort(nums, numsSize, sizeof(int), cmp);return (nums[numsSize - 1] - 1) * (nums[numsSize -2] - 1);
}

第二题 485. 最大连续 1 的个数

https://leetcode.cn/problems/max-consecutive-ones/description/
遍历数组,将1全部加起来,出现0就重置。

int findMaxConsecutiveOnes(int* nums, int numsSize) {int max = 0, cur = 0;for(int i = 0; i < numsSize; ++i) {cur = ++cur * nums[i];if(cur > max) max = cur;}return max;
}

这一个和上面是一样的思路,只是实现不同而已

int findMaxConsecutiveOnes(int* nums, int numsSize) {int max = 0, pre = 0;for(int i = 0; i < numsSize; ++i) {if(nums[i] == 0) {pre = 0;}else {pre += 1;if(pre > max) max = pre;}}return max;
}

第三题 2057. 值相等的最小索引

https://leetcode.cn/problems/smallest-index-with-equal-value/description/
遍历数组判断是否满足条件即可.

int smallestEqual(int* nums, int numsSize) {for(int i = 0; i < numsSize; ++i) {if(i % 10 == nums[i]) return i;}return -1;
}

第四题 27. 移除元素

https://leetcode.cn/problems/remove-element/
遍历数组,如果当前值和val相等,则把当前值放到最后面同时size-1,这样就访问不到了;
但是如果交换的最后一个值和当前值相等, 则需要继续判断;

int removeElement(int* nums, int numsSize, int val) {for(int i = 0; i < numsSize; ++i) {while(i < numsSize && nums[i] == val) {int temp = nums[i];nums[i] = nums[numsSize - 1];nums[numsSize - 1] = temp;--numsSize;}}return numsSize;
}

第五题 665. 非递减数列

https://leetcode.cn/problems/non-decreasing-array/description/
第一遍错误做法:

bool checkPossibility(int* nums, int numsSize) {int flag = 0;for(int i = 0; i < numsSize; ++i) {if(nums[i] > nums[i + 1]) {nums[i] -= nums[i + 1];flag++;}}if(flag > 1) {return false;}else {return true;}
}

需要多加写条件判断,还是太年轻了~

bool checkPossibility(int* nums, int numsSize) {int flag = 0;int pos = -1;for(int i = 0; i < numsSize - 1; ++i) {if(nums[i] > nums[i + 1]) {pos = i;flag++;}}if(flag >= 2) return false;if(flag == 0) return true;if(pos == 0 || nums[pos - 1] <= nums[pos + 1]) return true;if(pos == numsSize - 2 || nums[pos] <= nums[pos + 2]) return true;return false;
}
http://www.lryc.cn/news/252572.html

相关文章:

  • 封装websocket并在vuejs中调用
  • 博捷芯:半导体芯片切割,一道精细工艺的科技之门
  • BiseNet实现遥感影像地物分类
  • 【SpringBoot系列】SpringBoot时间字段格式化
  • .net core 连接数据库,通过数据库生成Modell
  • 开发工具idea中推荐插件
  • [c++]—string类___深度学习string标准库成员函数与非成员函数
  • PHP 双门双向门禁控制板实时监控源码
  • 【源码解析】聊聊线程池 实现原理与源码深度解析(二)
  • 本地Lambda(SAM LI)+ MySQL(Docker)环境构筑注意点
  • Windows下打包C++程序无法执行:无法定位程序输入点于动态链接库
  • Android 12 打开网络ADB并禁用USB连接ADB
  • 基于Langchain的txt文本向量库搭建与检索
  • vue2-router
  • css新闻链接案例
  • Android wifi连接和获取IP分析
  • MLIR笔记(5)
  • abapgit 安装及使用
  • 园区无线覆盖方案(智慧园区综合解决方案)
  • 配置中心--Spring Cloud Config
  • 笔记-模拟角频率和数字角频率的关系理解
  • Zookeeper+Kafka集群
  • Sunshine+Moonlight+Android手机串流配置(局域网、无手柄)
  • 从顺序表中删除具有最小值的元素(假设唯一) 并由函数返回被删元素的值。空出的位 置由最后一个元素填补,若顺序表为空,则显示出错信息并退出运行。
  • 详解—[C++ 数据结构]—AVL树
  • 卷积神经网络(CNN):乳腺癌识别.ipynb
  • 有文件实体的后门无文件实体的后门rootkit后门
  • GPT实战系列-大模型训练和预测,如何加速、降低显存
  • SQL Sever 基础知识 - 数据排序
  • vscode配置使用 cpplint