代码如下:(类似于编程之美的2.17,数组循环移位)


 static void Main(string[] args)
{ string input = "Hello World Welcome";
char[] tempArray = input.ToCharArray(); string result = RightShift(tempArray, , tempArray.Length-);
} public static string RightShift(char[] arrary, int startIndex, int endIndex)
{
Reverse(arrary, , endIndex); startIndex = endIndex = ; while (startIndex < arrary.Length - )
{
if (arrary[startIndex] == ' ')
{
startIndex++;
endIndex++;
continue; }
else if (arrary[endIndex] == ' ')
{
int currentIndex = endIndex;
Reverse(arrary, startIndex, endIndex - );
startIndex = endIndex = currentIndex;
}
else if (endIndex == arrary.Length - )
{
int currentIndex = endIndex;
Reverse(arrary, startIndex, endIndex);
startIndex = endIndex = currentIndex;
}
else
{
endIndex++;
}
} StringBuilder builder = new StringBuilder();
foreach (char item in arrary)
{
builder.Append(item);
} return builder.ToString(); } public static void Reverse(char[] arrary, int start, int end)
{
while (start < end)
{
char temp = arrary[start];
arrary[start] = arrary[end];
arrary[end] = temp; start++;
end--;
}
}

 

编程之美的2.17,数组循环移位 & 字符串逆转(反转) Hello world Welcome => Welcome world Hello的更多相关文章

  1. 编程之美 set 7 求数组中的最长递增子序列

    解法 1. 假设在目标数组 array[] 的前 i 个元素中, 最长递增子序列的长度为 LIS[i] 那么状态转移方程为 LIS[i] = max(1, LIS[k]+1) array[i+1] & ...

  2. 编程之美 set 5 寻找数组中最大值和最小值

    解法 1. 设置 min, max 两个变量, 然后遍历一遍数组, 比较次数为 2*N 2. 依然设置 min, max 两个变量并遍历数组, 但将遍历的 step 设置为 2, 比较次数为 1.5 ...

  3. [.net 面向对象编程基础] (17) 数组与集合

    [.net 面向对象编程基础] (17) 数组与集合 学习了前面的C#三大特性,及接口,抽象类这些相对抽象的东西以后,是不是有点很累的感觉.具体的东西总是容易理解,因此我们在介绍前面抽象概念的时候,总 ...

  4. 【编程之美】2.5 寻找最大的k个数

    有若干个互不相等的无序的数,怎么选出其中最大的k个数. 我自己的方案:因为学过找第k大数的O(N)算法,所以第一反应就是找第K大的数.然后把所有大于等于第k大的数取出来. 写这个知道算法的代码都花了2 ...

  5. 【编程之美】超时重传,滑动窗口,可靠性传输原理C语言实现

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://www.cnblogs.com/lihuidashen/p/128003 ...

  6. 【编程之美】CPU

    今天开始看编程之美 .第一个问题是CPU的使用率控制,微软的问题果然高大上,我一看就傻了,啥也不知道.没追求直接看答案试了一下.发现自己电脑太好了,4核8线程,程序乱飘.加了一个进程绑定,可以控制一个 ...

  7. 编程之美_1.1 让CPU占用率曲线听你指挥

    听到有人说让要写一个程序,让用户来决定Windows任务管理器的CPU占用率. 觉得很好奇.但第一个想法就是写个死循环.哈哈.不知道具体的占用率是多少,但至少能保证在程序运行时,CPU的占用率终会稳定 ...

  8. [质疑]编程之美求N!的二进制最低位1的位置的问题

    引子:编程之美给出了求N!的二进制最低位1的位置的二种思路,但是呢?但是呢?不信你仔细听我道来. 1.编程之美一书给出的解决思路 问题的目标是N!的二进制表示中最低位1的位置.给定一个整数N,求N!二 ...

  9. 编程之美 两个叶子的节点之间 最大距离 变种 leecode

    提交地址: https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 说一下思路http://www.cnblogs.com/mil ...

随机推荐

  1. JQuery时间轴timeline插件的学习-Lateral On-Scroll Sliding with jQuery+technotarek / timeliner

    一.Lateral On-Scroll Sliding with jQuery的使用 View demo      Download source 1. HTML结构 <div id=" ...

  2. (转)onTouchEvent方法的使用

      (转)onTouchEvent方法的使用 手机屏幕事件的处理方法onTouchEvent.该方法在View类中的定义,并且所有的View子类全部重写了该方法,应用程序可以通过该方法处理手机屏幕的触 ...

  3. 自己定制ListView,上拉刷新和下拉刷新,加载网络图片,并且添加缓存机制。

    package com.lixu.listviewrefresh; import java.util.ArrayList; import java.util.HashMap; import java. ...

  4. sql取年月日

    Sql Server 中一个非常强大的日期格式化函数 Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CON ...

  5. 【工具推荐】ELMAH——可插拔错误日志工具

    今天看到一篇文章(构建ASP.NET网站十大必备工具(2)),里面介绍了一个ELMAH的错误日志工具,于是研究了一下. ELMAH 是 Error Logging Modules and Handle ...

  6. bzoj 1816: [Cqoi2010]扑克牌

    #include<cstdio> #include<iostream> using namespace std; ],ans; bool pan(int x) { int a1 ...

  7. hdu 4630 No Pain No Game

    http://acm.hdu.edu.cn/showproblem.php?pid=4630 离散化+树状数组 将数组 *a  从后向前遍历 遍历到 a[x] 的时候 再枚举a[x]的约数 假如 约数 ...

  8. C++string的操作

    #include <iostream> using namespace std; int main() { //initilization string str("abc.ddd ...

  9. Program E-- CodeForces 18C

    Description Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In eac ...

  10. Python File I/O

    File is a named location on disk to store related information. It is used to permanently store data ...