反转字符串数组的元素

时间:2013-09-06 09:13:20

标签: c# algorithm reverse arrays

string[] myString = {"a","b","c","d"}
//Reverse string algorithm here
myString = {"d","c","b","a"}
在没有任何临时变量或.NET类,字符串方法等帮助的情况下,我被要求在访谈中这样做,以反转相同字符串数组的元素。有人告诉我使用像循环这样的基本编程结构。因为,我今天要接受另一次采访,我急着想知道这是否真的可行,因为我找不到解决办法。

14 个答案:

答案 0 :(得分:2)

由于你不能使用临时变量,我能想到的最好是先附加字符串然后再删除附加部分:

// append last strings to first strings
for(int i = 0; i < myString.Length / 2; i++)
{
    myString[i] = myString[i] + myString[myString.Length - i - 1];
}

// copy first half to last half
for(int i = myString.Length / 2 + 1; i < myString.Length; i++)
{
    myString[i] = myString[myString.Length - i - 1]
                      .SubString(0, 
                              myString[myString.Length - i - 1].Length 
                               - myString[i].Length);
}

// remove useless part from first half
for(int i = 0; i < myString.Length / 2; i++)
{
    myString[i] = myString[i].SubString(
                                  myString[myString.Length - i - 1].Length 
                                   - myString[i].Length);
}

愚蠢的做法?是。但不涉及其他变量。

答案 1 :(得分:1)

抱歉,我发错了答案......这是经过验证的答案:

     int k = len - 1;
    for(int i = 0; i<len/2; i++)
    {
        myString[i] = myString[i]+"."+myString[k--];
    }
    for(int i = len/2; i<len; i++)
    {
        myString[i] = myString[k].substring(0, 1);
        myString[k] = myString[k--].substring(2,3);
    }

但是,只考虑这是一个伪代码......我没有检查.NET语法。

答案 2 :(得分:1)

string[] myString = {"a","b","c","d"}

for(int = (myString.Length - 1); int >= 0; i--) {
    string rev =  myString[i];
    Console.Write(rev);
}

答案 3 :(得分:1)

面试中正确的答案是“为什么不使用类库?”但是然后说:“好吧,如果我需要编写自定义方法,因为这些库不支持需求……”。然后,我将展示两种方法,并争论何时使用每种方法。如果他们对此解释有疑问,那么我还是不想在那里工作。

带库:

string[] myString = {"a","b","c","d"};
List<string> list = myString.ToList();
list.Reverse();
myString = list.ToArray();

没有:

string[] myString = {"a","b","c","d"};
string[] newString = new string[myString.Length];
for (int i = 0, j = myString.Length - 1; i < myString.Length && j >= 0; i++, j--)
{
    newString[j] = myString[i];
}
myString = newString;

答案 4 :(得分:0)

你试过这个吗?

        string[] myString = { "a", "b", "c", "d","e","f" };

        int _firstcounter = 0;
        int _lastcounter = myString.Length-1;

        while (_firstcounter<=_lastcounter)
        {
            myString[_firstcounter] += myString[_lastcounter];
            myString[_lastcounter] = "" + myString[_firstcounter][0];
            myString[_firstcounter] = "" + myString[_firstcounter][1];

            _firstcounter++;
            _lastcounter--;
        }

答案 5 :(得分:0)

这不是一个完整的答案,也许是一个想法..

可以通过数学交换两个数字

swap(a,b)
   a = a + b
   b = a - b
   a = a - b

最初我建议可以使用字符ascii值来完成..然后我注意到字符串。

是否可以接受
swap(str1, str2)
    str1 = str1+str2
    str2 = str1[0]
    str1 = str1[1]

我希望你明白这个想法

答案 6 :(得分:0)

如果数组的最大字符串长度小于10,那么可能会有用....

string[] myString = { "aaaa", "bbb", "ccccccc", "dddddd", "e", "fffffffff" };
for (int i = 0; i < myString.Length; i++)
{
    myString[i] = myString[i].Length.ToString() + myString[i]; 
}
for (int i = 0; i < myString.Length/2; i++)
{
    myString[i] = myString[i] + myString[myString.Length-1-i];
    myString[myString.Length - 1 - i] = myString[i];
}
for (int i = 0; i < myString.Length/2; i++)
{
     myString[i] = myString[i].Substring(int.Parse(myString[i][0].ToString())+2,int.Parse(myString[i][int.Parse(myString[i][0].ToString())+1].ToString()));                       
}
for (int i = myString.Length / 2; i < myString.Length; i++)
{
     myString[i] = myString[i].Substring(1, int.Parse(myString[i][0].ToString()));
}

答案 7 :(得分:0)

试试这个。

public static class ExtensionMethods
{
    public static IEnumerable<T>  ReverseArray<T>(this T[] source)
    {
        for (int i = source.Length - 1; i >= 0; i--)
        {
            yield return source[i];
        }
    }

    public static T[] EnumerableToArray<T>(this IEnumerable<T> source)
    {
        var array = new T[source.Count()];

        int k = 0;
        foreach (var n in source)
        {
            array[k++] = n;
        }

        return array;
    }
}

使用示例

    public static void Main(string[] args)
    {
        string[] myString = {"a", "b", "c", "d"};

        myString = myString.ReverseArray().EnumerableToArray();
    }

答案 8 :(得分:0)

static void ReverseMyString(string[] myString)
        {
        int start=0, end= myString.Length-1;
        string temp = "";

        while (start < end)
        {
            temp = myString[start];
            myString[start] = myString[end];
            myString[end] = temp;
            start++;
            end--;
        }
    }

答案 9 :(得分:-1)

是的,你可以做到 -

string[] myString = { "a", "b", "c", "d" };
myString  = (from a in myString orderby a descending select a).ToArray();

答案 10 :(得分:-1)

您需要至少一个变量来交换值。

在伪代码中:

for(i : 0..n/2) {  
    // swap s[i] and s[n-i]
    String tmp = s[i];
    s[i] = s[n-i];
    s[n-i] = tmp;
}

答案 11 :(得分:-2)

我们可以使用Array.Reverse(UrArray);

答案 12 :(得分:-3)

在MSDN上查看Array.Reverse方法。

答案 13 :(得分:-3)

string[] myString = {"a","b","c","d"};
string[] reversed = new string[myString.Length];

for(int i = 0; i<myString.Length; i++) {
    reversed[i] = myString[myString.Length -i-1];
}
myString = reversed;