当前位置:网站首页>优化是一种习惯●出发点是“站在靠近临界“的地方
优化是一种习惯●出发点是“站在靠近临界“的地方
2022-08-11 04:44:00 【一 乐】
1、举例:数组的动态插入、删除
站在临界值的角度考虑
这个临界值,不一定是最后一个元素

- 删除某个元素:

第一种做法:取到最大值:i=size-1时,表达式为:elements[size-2]=elements[size-1]
第二种做法:取到最大值:i=size-1时,表达式为:elements[size-1]=elements[size] 明显不符合设计了,例如当前数组有size个元素,那么最后一个元素应该是 elements[size-1]
其实越界也是思考角度,站在被覆盖角度的话,那么临界的最后一个被覆盖元素,是当前数组的倒数第二个元素 elements[size-2] 呀
第二种做法:应该修改一下循环判断条件才正确:for(int i = index; i < size-1; i++)
第一种做法:是站在覆盖者角度,也站在了靠近‘临界值’(靠近最后一个元素)的角度,不容易出错,推荐
- 添加某个元素:

在添加元素这里:做法1和做法2都没有问题,不过建议,
统一习惯:出发点是"站在靠近临界"的地方
边栏推荐
- AVH 动手实践 (二) | 在 Arm 虚拟硬件上部署 PP-OCR 模型
- 监听U盘插入 拔出 消息,获得U盘盘符
- c语言fprintf、fscanf、sscanf以及sprintf函数知识要点总结
- findViewById返回null的问题
- 洛谷P2370 yyy2015c01 的 U 盘
- Get Qt installation information: including installation directory and various macro addresses
- Common layout effect realization scheme
- [Note] Is the value of BatchSize the bigger the better?
- 交换机和路由器技术-36-端口镜像
- Embedded Sharing Collection 33
猜你喜欢

Object Creation and Display Transformation

梅克尔工作室--OpenEuler培训笔记(1)

I wrote some code in my resume, but I still can't pass the interview

Self-research capability was recognized again, and Tencent Cloud Database was included in the Forrester Translytical report

ALSA音频架构 -- snd_pcm_open函数分析

如何将360全景图导出高清短视频分享到视频平台上?

【组成原理 九 CPU】

交换机和路由器技术-31-扩展ACL

如何给网页添加icon图标?

网络安全培训机构哪家好?排名怎么选择?
随机推荐
力扣——青蛙跳台阶问题
《卫星界》刊评“星辰大海”计划:孙宇晨为太空旅游带来新的机遇
Application of Identification Cryptography in IMS Network
[Server installation mysql] Use mysql offline installation package to install mysql5.7 under centos7
剑指offer_抽象建模能力
如何缓解压力、拒绝内耗【1】
[Likou] 22. Bracket generation
洛谷P1196 银河英雄传说
每日一题-滑动窗口
set_new_handler(0)是什么意思?有什么用?
洛谷P2370 yyy2015c01 的 U 盘
【ImageNet】数据集1000个类的名称
解决多线程调用sql存储过程问题
Selenium自动化测试框架工作原理你明白了吗?
洛谷P4061 大吉大利,晚上吃鸡
洛谷P4560 Wall 砖墙
洛谷P4324 扭动的回文串
洛谷P1763 埃及分数
To break the bottleneck of transactional work, the gentleman signs the electronic contract to release the "source power" of HR!
直播平台开发,Flutter,Drawer侧滑