两种字符移位算法

/// <summary>
/// 字符左移b位
/// </summary>
/// <param name="b"></param>
public static void MoveBit(int b)
{
//产生字符数组。
char[] cs = new char[8];
for (int i = 0; i < 8; i++)
{
cs[i]=(char)(65+i);
}
//临时存储数组
char[] temp = new char[8];
for (int i = b; i < 8; i++)
{
var t = cs[i - b];
temp[i - b] = t;
cs[i - b] = cs[i];
}
for (int i = 8-b,j=0; i < 8; i++,j++)
{
cs[i]=temp[j];
}
string s = new string(cs);
MessageBox.Show(s);
}

/// <summary>
/// 翻转字符串
/// </summary>
/// <param name="cs"></param>
/// <param name="s">起始位置</param>
/// <param name="e">结束位置</param>
public static void Reverse(char[] cs, int s, int e)
{
int m = (s + e) / 2;
char temp;
for (int i = s, j = e; i <= m; i++, j--)
{
temp = cs[i];
cs[i] = cs[j];
cs[j] = temp;
}
}

/// <summary>
/// 通过翻转实现移位,o(n)。不需要临时存储空间。
/// </summary>
/// <param name="s"></param>
/// <param name="n"></param>
public static void MoveBit(char[] s, int n)
{
Reverse(s, 0, n-1);
Reverse(s, n, s.Length-1);
Reverse(s, 0, s.Length - 1);
}

转载于:https://www.cnblogs.com/363546828/archive/2013/05/24/3097455.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值