ReverseString,一个C#的采访,问题 [英] ReverseString, a C# interview-question

查看:127
本文介绍了ReverseString,一个C#的采访,问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个面试问题是问我的上一段代码初级程序员写的反馈。他们暗示有可能会出现问题,并表示将在很大程度上对大字符串中使用。

I had an interview question that asked me for my 'feedback' on a piece of code a junior programmer wrote. They hinted there may be a problem and said it will be used heavily on large strings.

public string ReverseString(string sz)
{
    string result = string.Empty;
    for(int i = sz.Length-1; i>=0; i--)
    {
      result += sz[i]
    }
    return result;
}



我无法发现它。我没有看到任何问题。
现在回想起来,我可以说,用户应该调整,但它看起来像C#没有一个调整大小(我是一个C ++的家伙)。

I couldn't spot it. I saw no problems whatsoever. In hindsight I could have said the user should resize but it looks like C# doesn't have a resize (i am a C++ guy).

我结束高达写作之类的东西,如果可能使用迭代器,[X]在容器中不能随机访问,因此它可能会很慢。和杂事。但我肯定地说我从来没有优化的C#代码,所以我的想法可能还没有让我失望了采访。

I ended up writing things like use an iterator if its possible, [x] in containers could not be random access so it may be slow. and misc things. But I definitely said I never had to optimize C# code so my thinking may have not failed me on the interview.

我想知道,什么是这个代码的问题?,做你们看到它

I wanted to know, what is the problem with this code, do you guys see it?

-edit -

-edit-

我变成了一个wiki这一点,因为可以有几个正确的答案。
此外,我很高兴我明确表示我从来没有优化的C#程序,并提到的杂项其他的事情。哎呀。我一直以为C#剪掉与这些类型的东西任何性能问题。 。哎呀

I changed this into a wiki because there can be several right answers. Also i am so glad i explicitly said i never had to optimize a C# program and mentioned the misc other things. Oops. I always thought C# didnt have any performance problems with these type of things. oops.

推荐答案

这是到目前为止给出的答案几点意见:

A few comments on the answers given so far:


  • 他们每一个人(至少到目前为止)都将失败代理对和组合字符。呵呵的Unicode的乐趣。倒车字符串是不一样倒车字符的序列。

  • 我喜欢的马克对空,空优化和单个字符输入。尤其是,不仅此得到正确的答案很快,但它也处理空值(其中没有其他答案做)

  • 我原本以为 ToCharArray 然后按 Array.Reverse 将是最快的,但它确实创建一个垃圾的副本。

  • 的StringBuilder 解决方案创建一个字符串(而不是字符数组),并操纵了,直到您调用的ToString 。有没有涉及额外的复制......但还有更多的工作保持长度等。

  • Every single one of them (so far!) will fail on surrogate pairs and combining characters. Oh the joys of Unicode. Reversing a string isn't the same as reversing a sequence of chars.
  • I like Marc's optimisation for null, empty, and single character inputs. In particular, not only does this get the right answer quickly, but it also handles null (which none of the other answers do)
  • I originally thought that ToCharArray followed by Array.Reverse would be the fastest, but it does create one "garbage" copy.
  • The StringBuilder solution creates a single string (not char array) and manipulates that until you call ToString. There's no extra copying involved... but there's a lot more work maintaining lengths etc.

这是更有效的解决方案吗?好吧,我不得不基准它有什么想法的话 - 但即便如此,这不是要告诉整个故事。你在高内存压力,其中多余的垃圾是一个真正的痛苦的情况下使用呢?有多快你的内存VS你的CPU等

Which is the more efficient solution? Well, I'd have to benchmark it to have any idea at all - but even so that's not going to tell the whole story. Are you using this in a situation with high memory pressure, where extra garbage is a real pain? How fast is your memory vs your CPU, etc?

与以往一样,可读性的一般的王 - 它没有比好很多马克的在这一方面的答案。特别是,还有的无房的一个差一错误,而我不得不居然花了心思验证答案。我不喜欢思考。它伤害了我的大脑,所以我尽量不这样做非常频繁。使用内置的 Array.Reverse 听起来要好得多给我。 (好吧,所以它仍然无法对代理人等,但嘿...)

As ever, readability is usually king - and it doesn't get much better than Marc's answer on that front. In particular, there's no room for an off-by-one error, whereas I'd have to actually put some thought into validating the other answers. I don't like thinking. It hurts my brain, so I try not to do it very often. Using the built-in Array.Reverse sounds much better to me. (Okay, so it still fails on surrogates etc, but hey...)

这篇关于ReverseString,一个C#的采访,问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆