当前位置:网站首页>Optimization is a habit The starting point is to 'stand close to the critical'

Optimization is a habit The starting point is to 'stand close to the critical'

2022-08-10 19:02:00 Yilele


1. Example: dynamic insertion and deletion of arrays

  • Consider the critical value

    This critical value, not necessarily the last element


  • Delete an element:

The first method: when the maximum value is obtained: i=size-1, the expression is: elements[size-2]=elements[size-1]

Second approach: When the maximum value is obtained: i=size-1, the expression is: elements[size-1]=elements[size] This is obviously not in line with the design. For example, the current array has size elements, thenThe last element should be elements[size-1]

In fact, crossing the boundary is also a thinking angle. If you stand at the covered angle, then the last covered element in the critical is the penultimate element of the current array elements[size-2]

The second approach: The loop judgment condition should be modified to be correct: for(int i = index; i < size-1; i++)

The first approach is to stand at the cover angle, and also stand close to the 'threshold' (closer to the last element)

The angle of font>, not easy to make mistakes, recommended



  • Add an element:

In adding elements here: There is no problem with practice 1 and practice 2, but it is recommended,

Uniform Habits: The starting point is to "stand near critical"

原网站

版权声明
本文为[Yilele]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101837599844.html