当前位置:网站首页>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 array
Only 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 array
Only 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
边栏推荐
猜你喜欢
Day113. Shangyitong: user authentication, Alibaba Cloud OSS, patient management
Emotional crisis, my friend's online dating girlfriend wants to break up with him, ask me what to do
透过开发抽奖小程序,体会创新与迭代
感情危机,朋友的网恋女友要和他闹分手,问我怎么办
NowCoderTOP17-22 Binary search/sort - continuous update ing
sql力扣刷题八
nodeJs--url模块
双链表的插入和删除
C#之泛型、委托、事件及其使用
开放麒麟 openKylin 自动化开发者平台正式发布
随机推荐
Use turtle to draw buttons
Summary of three methods for SQL deduplication
使用turtle画按钮
Dart Log tool class
Burndown chart of project management tools: Dynamic assessment of team work ability
Centos7 install mysql5.7
NowCoderTOP28-34二叉树——持续更新ing
双链表的插入和删除
unity-shader-2
“chmod 777-R 文件名”什么意思?
金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--开放虚拟网络(OVN)简介(课后练习)
恋爱期间的赠与能否撤销
VMware下安装win10启动后进入Boot Manger界面如何解决
Redis集群-哨兵模式原理(Sentinel)
C#多态的实现
loadrunner-Controller负载测试-各模块功能记录01测试场景设计
单点登录的三种方式
解决rpc error: code = Unimplemented desc = method CheckLicense not implemented
Rich text editor Tinymce
Three ways of single sign-on