也来纠结一下字符串翻转

   1:  class Program
   2:  {
   3:      static void Main(string[] args)
   4:      {
   5:          Program p = new Program();
   6:          string r = p.ReverseString("5毛1234\0");
   7:          r = p.ReverseString(string.Empty);
   8:          r = p.ReverseString("1234567890",0,9);
   9:   
  10:          Console.ReadKey();
  11:      }
  12:   
  13:      /// <summary>
  14:      /// 翻转字符串,效率比较高,时间复杂度O(n/2)
  15:      /// 由此引申出更灵活的实现,见下方重载
  16:      /// </summary>
  17:      public string ReverseString(string s)
  18:      {
  19:          if (s == null)
  20:          {
  21:              return null;
  22:          }
  23:   
  24:          int len = s.Length;
  25:          char[] charArray = s.ToArray();
  26:          for (int i = 0; i < len / 2; i++)
  27:          {
  28:              char temp = charArray[i];
  29:              charArray[i] = charArray[len - i - 1];
  30:              charArray[len - i - 1] = temp;
  31:          }
  32:          
  33:          return new string(charArray);
  34:      }
  35:   
  36:      /// <summary>
  37:      /// 可以翻转指定范围子串
  38:      /// 于是之上的Reverse可复用该方法:Reverse(s,0,s.Length)
  39:      /// </summary>
  40:      /// <param name="s">待翻转字符串</param>
  41:      /// <param name="start">起始位置</param>
  42:      /// <param name="end">结束为止</param>
  43:      /// <returns></returns>
  44:      public string ReverseString(string s,int start,int end)
  45:      {
  46:          if (string.IsNullOrEmpty(s) || s.Length==1)
  47:          {
  48:              return s;
  49:          }
  50:   
  51:          if (s.Length <= end)
  52:          {
  53:              throw new ArgumentException("end should be less than string length");
  54:          }
  55:   
  56:          if (start < 0)
  57:          {
  58:              throw new ArgumentException("start should be larger than 0 or equal to 0");
  59:          }
  60:   
  61:          if (start >= end)
  62:          {
  63:              throw new ArgumentException("start should be less than end");
  64:          }
  65:   
  66:          char[] charArray = s.ToArray();
  67:          for (int i = start; i <(start+end+1)/2; i++)
  68:          {
  69:              char temp = charArray[i];
  70:              charArray[i]=charArray[end-i+start];
  71:              charArray[end-i+start] = temp;
  72:          }
  73:          return new string(charArray);
  74:      }
  75:   
  76:      /// <summary>
  77:      /// 别忘了我们是在C# Coding!需要这么原始嘛?
  78:      /// Array本身就有反转的方法
  79:      /// </summary>
  80:      public string ReverseString_Simple(string s, int start, int end)
  81:      {
  82:          if (string.IsNullOrEmpty(s))
  83:          {
  84:              return s;
  85:          }
  86:          char[] charArray = s.ToArray();
  87:   
  88:          return new string(charArray.Reverse().ToArray());
  89:      }
  90:   
  91:      /*****
  92:      *难道对类库本身的实现不感兴趣嘛?当然No!
  93:      *我们看到类库的实现如下,和之上的实现思想毫无二致
  94:      public static void Reverse(Array array, int index, int length)
  95:      {
  96:          if (array == null)
  97:          {
  98:              throw new ArgumentNullException("array");
  99:          }
 100:          if ((index < array.GetLowerBound(0)) || (length < 0))
 101:          {
 102:              throw new ArgumentOutOfRangeException((index < 0) ? "index" : "length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
 103:          }
 104:          if ((array.Length - (index - array.GetLowerBound(0))) < length)
 105:          {
 106:              throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
 107:          }
 108:          if (array.Rank != 1)
 109:          {
 110:              throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
 111:          }
 112:          if (!TrySZReverse(array, index, length))
 113:          {
 114:              int num = index;
 115:              int num2 = (index + length) - 1;
 116:              object[] objArray = array as object[];
 117:              if (objArray == null)
 118:              {
 119:                  while (num < num2)
 120:                  {
 121:                      object obj3 = array.GetValue(num);
 122:                      array.SetValue(array.GetValue(num2), num);
 123:                      array.SetValue(obj3, num2);
 124:                      num++;
 125:                      num2--;
 126:                  }
 127:              }
 128:              else
 129:              {
 130:                  while (num < num2)
 131:                  {
 132:                      object obj2 = objArray[num];
 133:                      objArray[num] = objArray[num2];
 134:                      objArray[num2] = obj2;
 135:                      num++;
 136:                      num2--;
 137:                  }
 138:              }
 139:          }
 140:      }
 141:      
 142:      如果你还纠结TrySZReverse,
 143:      打开Rotor,可以看到TrySZReverse的native实现映射到这样一个函数
 144:      static void Reverse(KIND array[], UINT32 index, UINT32 count) {
 145:          LEAF_CONTRACT;
 146:  
 147:          _ASSERTE(array != NULL);
 148:          if (count == 0) {
 149:              return;
 150:          }
 151:          UINT32 i = index;
 152:          UINT32 j = index + count - 1;
 153:          while(i < j) {
 154:              KIND temp = array[i];
 155:              array[i] = array[j];
 156:              array[j] = temp;
 157:              i++;
 158:              j--;
 159:          }
 160:      }
 161:      *****/
 162:  }
 163:   
posted @ 2013-12-25 20:27  Dance With Automation  Views(289)  Comments(0Edit  收藏  举报