• [面试题]去除字符串中相邻两个字符的重复


     1/// <summary>
     2        /// 去除字符串中相邻两个字符的重复 
     3        /// 
     4        /// exp.   
     5        ///    abb -> ab
     6        ///    abbccaabbcc -> abcabc
     7        ///    aaabbb -> ab
     8        /// </summary>
     9        /// <param name="strInput"></param>
    10        /// <returns></returns>

    11        static string GetStringOfSingleChar(string strInput)
    12        {
    13            if (strInput == null)
    14                throw new Exception("String cannot be null.");
    15
    16            int Length = 0;
    17            if ((Length = strInput.Length) < 2return strInput;
    18            char[] strArray = strInput.ToCharArray();
    19
    20            int count = 0;
    21            for (int i = 1; i < Length; i++)
    22            {
    23                if (strArray[i] != strArray[i - 1]) continue;
    24
    25                char cTemp = strArray[i];
    26                for (int j = i; j < Length - 1; j++)
    27                    strArray[j] = strArray[j + 1];
    28                strArray[Length - 1= cTemp;
    29
    30                if (i + count >= Length) break;
    31                i--;
    32                count++;
    33            }

    34
    35            return new string(strArray , 0 , Length - count);
    36        }
  • 相关阅读:
    原创frame-relay配置
    iptables详解和练习
    nfs-rpcbind-portmap挂载nfs-network file system
    linux-user-group添加与删除
    cgi-fastcgi-fpm
    lamp介绍
    子签CA以及给别人发CA
    正则表达式
    字符集和字符编码
    C++11新特性
  • 原文地址:https://www.cnblogs.com/sskset/p/715364.html
Copyright © 2020-2023  润新知