zoukankan      html  css  js  c++  java
  • 两种字符移位算法

    /// <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);
    }

  • 相关阅读:
    w3c标准
    HTML+CSS理解
    Mybatis源码日记(一)
    Sqlmap学习笔记(三)
    EasyExcel读取跨行单元格数据为空的解决办法
    Sqlmap学习笔记(二)
    Sqlmap学习笔记(一)
    Linux安装运行Typora
    Kafka环境搭建
    Mysql主从配置
  • 原文地址:https://www.cnblogs.com/363546828/p/3097455.html
Copyright © 2011-2022 走看看