string[] myString = {"a","b","c","d"}
//Reverse string algorithm here
myString = {"d","c","b","a"}
在没有任何临时变量或.NET类,字符串方法等帮助的情况下,我被要求在访谈中这样做,以反转相同字符串数组的元素。有人告诉我使用像循环这样的基本编程结构。因为,我今天接受另一次采访,我急着知道这是否真的可行,因为我找不到解决方法。
在这里,没有支持变量,没有.net函数:)但它假设数组中的所有字符串都具有长度1(就像它们在您发布的代码中所做的那样)。
string[] myString = {"a","b","c","d", "e"};
for(int i = 0; i < myString.Length/2; i++)
{
myString[i] += myString[myString.Length -1 -i];
myString[myString.Length -1 -i] = ""+myString[i][0];
myString[i] = "" + myString[i][1];
}
是的,你可以这样做 -
string[] myString = { "a", "b", "c", "d" };
myString = (from a in myString orderby a descending select a).ToArray();
您至少需要一个变量来交换值。
在伪代码中:
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;
}
我们可以使用Array.Reverse(UrArray);
在MSDN上查看Array.Reverse方法。
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;
由于你不能使用临时变量,我能想到的最好是先附加字符串,然后再删除附加部分:
// 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);
}
愚蠢的做法?是。但不涉及其他变量。
对不起,我发错了答案......这是经过验证的答案:
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语法。
string[] myString = {"a","b","c","d"}
for(int = (myString.Length - 1); int >= 0; i--) {
string rev = myString[i];
Console.Write(rev);
}
这不是一个完整的答案,也许是一个想法..
可以通过数学交换两个数字
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]
我希望你明白这个主意
你试过这个吗?
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--;
}
如果数组的最大字符串长度小于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()));
}
试试这个。
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();
}
你面试中的正确答案是“你为什么不使用班级图书馆?”但后来说,“好吧,如果我需要编写一个自定义的方法,因为库不支持需要......”。然后我会展示这两种方法并争论何时使用每种方法。如果他们对这个解释有问题,那么无论如何我都不想在那里工作。
随着图书馆:
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;