当前位置:网站首页>Inversion problem - key point
Inversion problem - key point
2022-07-31 10:18:00 【[email protected]】
Given a linear table, how to invert the elements in it?Two integer variables i and j can be set, i points to the first element, j points to the last element, while exchanging the elements pointed to by i and j, and let i and j move toward each other until they meet, the implementation code is as follows.Suppose the elements are stored in the array a[ ], and left and right are the subscripts of the elements at both ends of the array.
for(int i=lefft,j=right;i<j;i++,j--){int temp=a[i];a[i]=a[j];a[j]=temp;}【Example 2-5】
(1) Move the front k(k (2) Move the front k(k (3) Move the elements in the array (X0, X1, ..., Xn-1) tobecomes (Xp, Xp+1, ..., Xn-1, X0, X1,...,Xp-1), that is, circularly shifted left by p(0Only need to invert the entire array to satisfy the requirement that the front-end k elements are reversed and placed in the back-end of the arrayOnly need to invert the front-end k elements, and then invert the entire array to satisfy the front-end k elements in the original order and put them in the back end of the arrayOnly need to set 0~p-1 The element at the position is reversed, and then p~n-1Invert the elements of the position, and then invert the entire array.
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/212/202207311008455709.html
边栏推荐
猜你喜欢
随机推荐
C#多态的实现
C#之泛型、委托、事件及其使用
darknet 硬件软件环境的设置和检测
win10镜像下载
NowCoderTOP17-22 二分查找/排序——持续更新ing
Sql优化总结!详细!(2021最新面试必问)
模块八
darknet 训练分类网络
What does "chmod 777-R filename" mean?
Principle of Redis Sentinel
unity-shader-2
loadrunner-Controller负载测试-各模块功能记录01测试场景设计
顺序表的删除
第七章
(C语言)程序环境和预处理
Dart Log tool class
第五章
[ 动词词组 ] 合集
踩水坑2 数据超出long long
实现线程池









