当前位置:网站首页>优化是一种习惯●出发点是“站在靠近临界“的地方
优化是一种习惯●出发点是“站在靠近临界“的地方
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都没有问题,不过建议,
统一习惯:出发点是"站在靠近临界"的地方
边栏推荐
- Pinduoduo store business license related issues
- ALSA音频架构 -- aplay播放流程分析
- Overview of the JVM garbage collection and mechanism
- Read the article, high-performance and predictable data center network
- Dry goods: The principle and practice of server network card group technology
- 嵌入式分享合集33
- JVM 垃圾回收的概述与机制
- Redis deletes keys in batches according to regular rules
- 【实战场景】商城-折扣活动设计方案
- 交换机和路由器技术-36-端口镜像
猜你喜欢

Get Qt installation information: including installation directory and various macro addresses

Dry goods: The principle and practice of server network card group technology

The principle, architecture, implementation, practice of "transfer" and "search", no need to be afraid of interviews

es-head plugin insert query and conditional query (5)

findViewById返回null的问题

Apache初体验

简历里写了会代码,却依然过不了面试这一关

(转)JVM中那些区域会发生OOM?

交换机--- 生成树--三层架构总结

What is machine learning?Explain machine learning concepts in detail
随机推荐
set_new_handler(0)是什么意思?有什么用?
Pinduoduo store business license related issues
Overview of the JVM garbage collection and mechanism
[Likou] 22. Bracket generation
洛谷P1763 埃及分数
Redis:解决分布式高并发修改同一个Key的问题
Layered Architecture & SOA Architecture
findViewById返回null的问题
c语言fprintf、fscanf、sscanf以及sprintf函数知识要点总结
ALSA音频架构 -- aplay播放流程分析
利用Navicat Premium导出数据库表结构信息至Excel
Mysql: set the primary key to automatically increase the starting value
【服务器安装mysql】centos7下使用mysql离线安装包安装mysql5.7
How to add icons to web pages?
洛谷P5139 z小f的函数
Where can machine learning be applied?What is machine learning useful for?
【yolov7系列三】实战从0构建训练自己的数据集
Research on a Consensus Mechanism-Based Anti-Runaway Scheme for Digital Trunking Terminals
使用百度EasyDL实现施工人员安全装备检测
走出迷宫的最短路径