当前位置:网站首页>优化是一种习惯●出发点是“站在靠近临界“的地方
优化是一种习惯●出发点是“站在靠近临界“的地方
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都没有问题,不过建议,
统一习惯:出发点是"站在靠近临界"的地方
边栏推荐
- Resize() usage row pit in vector
- 力扣——旋转数组的最小数字
- map and set - natural search and lookup semantics
- Application of Identification Cryptography in IMS Network
- Harvesting of radio frequency energy
- 洛谷P4032 火锅盛宴
- Self-research capability was recognized again, and Tencent Cloud Database was included in the Forrester Translytical report
- 如何缓解压力、拒绝内耗【1】
- To break the bottleneck of transactional work, the gentleman signs the electronic contract to release the "source power" of HR!
- Mysql:设置主键自动增长起始值
猜你喜欢

"104 Maximum Depth of Binary Trees" in LeetCode's Day 12 Binary Tree Series

Provincial level of Echart maps, as well as all prefecture-level download and use

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

自研能力再获认可,腾讯云数据库入选 Forrester Translytical 报告

交换机和路由器技术-32-命名ACL

LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"

交换机和路由器技术-36-端口镜像

干货:服务器网卡组技术原理与实践

Where can machine learning be applied?What is machine learning useful for?

Selenium自动化测试框架工作原理你明白了吗?
随机推荐
"125 Palindrome Verification" of the 10th day string series of LeetCode brushing questions
redis按照正则批量删除key
I wrote some code in my resume, but I still can't pass the interview
The shortest path out of the maze
Alibaba Cloud releases 3 high-performance computing solutions
JVM 垃圾回收的概述与机制
【组成原理 九 CPU】
Jetson Orin platform 4-16 channel GMSL2/GSML1 camera acquisition kit recommended
0 Basic software test for career change, self-study for 3 months, 12k*13 salary offer
澳大利亚网络空间安全体系建设论析
Listen to pull out U disk inserted into the message, U disk drive
Self-research capability was recognized again, and Tencent Cloud Database was included in the Forrester Translytical report
洛谷P4847 银河英雄传说V2
Add PRODUCT_BOOT_JARS and classes to provide jar packages to applications
Redis: Solve the problem of modifying the same key with distributed high concurrency
[Web3 series development tutorial - create your first NFT (9)] How to view your NFT in the mobile wallet
延长经济保险(jeecgboot)
WPF DataGrid 使用数据模板(2)
交换机和路由器技术-34-动态NAT
关于数据分页显示