简体   繁体   English

如何递归地反转字符串

[英]How to reverse a string recursively

I was in an interview the other day and I was asked to write a method that reverses a string recursively. 前几天我在一次采访中,被要求写一个递归地反转字符串的方法。

I started writing a method that calls itself and got stuck. 我开始编写一个调用自己的方法并陷入困境。

Here is what I was asked, reverse a string "Obama" recursively in JavaScript. 这是我被问到的,在JavaScript中递归地反转字符串“ Obama”。

Here is how far I got. 这是我走了多远。

function reverseString(strToReverse)
{

         reverseString(strToReverse);

};

And got stuck, they said NO for i loops. 被困住了,他们说我不循环。

Anyone got any ideas? 任何人有任何想法吗?

Look at it this way: the reversed string will start with the last letter of the original, followed by all but the last letter, reversed. 看看这样说:颠倒字符串将与原来的最后一个字母,后面的一切,但最后一个字母,颠倒启动

So: 所以:

function reverseString(strToReverse)
{
  if (strToReverse.length <= 1)
    return strToReverse;

  // last char +
  // 0 .. second-last-char, reversed
  return strToReverse[strToReverse.length - 1] + 
    reverseString( strToReverse.substring(0, strToReverse.length - 1) );
}

The simplest one: 最简单的一个:

 function reverse(input) { if (input == null || input.length < 2) return input; return reverse(input.substring(1)) + input.charAt(0); } console.log(reverse('Barack Obama')); 

See the solution by @MaxZoom below for a more concise version. 有关更简洁的版本,请参见下面的@MaxZoom解决方案

Note that the tail-recursive style in my own answer provides no advantage over the plain-recursive version since JS interpreters are not required to perform tail call elimination. 请注意,我自己的答案中的尾部递归样式与普通递归版本相比没有任何优势,因为不需要JS解释器执行尾部消除。

[Original] [原版的]

Here's a tail recursive version that works by removing a character from the front of the input string and prepending it to the front of the "accumulator" string: 这是一个尾部递归版本,其工作原理是从输入字符串的开头删除一个字符,然后将其放在“ accumulator”字符串的前面:

function reverse(s, acc) {
  if (s.length <= 0) { return acc; }
  return reverse(s.slice(1), s.charAt(0) + (acc||''));
}

reverse('foobar'); // => "raboof"

Single line solution. 单线解决方案。 And if they asked I tell them it's recursive in the native code part and not to ask any more stupid questions. 如果他们问,我告诉他们在本机代码部分是递归的,不要再问其他愚蠢的问题。

var result = "Obama".split('').reverse().join('');

Output: amabO 输出: amabO

The real problem here is not "how to reverse a string". 真正的问题不是“如何反转字符串”。 The real problem is, "do you understand recursion". 真正的问题是“您了解递归”。 That is what the interview question is about! 那就是面试问题!

So, in order to solve the problem, you need to show you know what recursion is about, not that you can reverse the string "Obama". 因此,为了解决此问题,您需要向您展示知道什么是递归,而不是可以反转字符串“ Obama”。 If all you needed to do was reverse the string "Obama", you could write return "amabO"; 如果您只需要反转字符串“ Obama”,则可以编写return "amabO"; see? 看到?

In other words, this specific programming task is not what it's all about! 换句话说,这个特定的编程任务不是全部! The real solution is not to copy and paste the code from the answers here, but to know about recursion. 真正的解决方案不是从此处的答案中复制并粘贴代码,而是要了解递归。

In brief, 简单来说,

  • Recursion involves calling the same function again, yes, but that's not all 递归涉及再次调用相同的函数,是的,但这还不是全部
  • In order to prevent stack overflow, you MUST ensure that the function doesn't call itself indefinitely 为了防止堆栈溢出,您必须确保该函数不会无限期地调用自身
  • So there's always a condition under which the function can exit without calling itself (again) 因此,总有一种情况,函数可以退出而不调用自身(再次)
  • And when it does call itself again, it should do so with parameters that make the above condition more likely. 并且当它再次调用自身时,应该使用使上述情况更可能发生的参数进行调用。

In the case of string operations, one way to make that all happen is to make sure that it calls itself only with strings that are shorter than the one it was called with. 对于字符串操作,一种使所有这些事情发生的方法是确保仅使用比被调用的字符串短的字符串来调用自身。 Since strings are not of an infinite length, the function can't call itself an infinite number of times that way. 由于字符串的长度不是无限的,因此该函数不能以这种方式无限次地调用自身。 So the condition can be that the string has a length of zero, in which case it's impossible to call itself with a shorter string. 因此,条件可能是字符串的长度为零,在这种情况下,不可能用较短的字符串来调用自身。

If you can prove that you know all this, and can use it in a real world program, then you're on your way to passing the interview. 如果您可以证明自己了解所有这些知识,并且可以在现实世界的程序中使用它,那么您就可以通过面试了。 Not if you copy and paste some source you found on the internet. 如果您复制并粘贴在Internet上找到的某些来源,则不会。

Hope this helps! 希望这可以帮助!

We can easily reverse a string in the recursion method using the ternary operator 我们可以使用三元运算符轻松地在递归方法中反转字符串

function reverseString(strToReverse) {

    return str.length > 1 ? reverse(str.slice(1)) + str.charAt(0) : str;
}

reverseString("America");

Not the smartest way to reverse a string, but it is recursive: 反向字符串不是最聪明的方法,但是它是递归的:

function reverse(input, output) {
    output = output || '';
    if (input.length > 0) {
        output = output.concat(input[input.length -1]);
        return reverse(input.substr(0, input.length - 1), output);
    }
    return output;
}

console.log(reverse('Obama'));

Here's a jsfiddle 这是一个jsfiddle

Maybe something like this? 也许是这样的吗?

var base = 'Obama',
    index = base.length,
    result = '';

function recursive(){
    if (index == 0) return;
    index -= 1;
    result += base[index];
    recursive();
}

recursive();

alert(result);

jsfiddle: https://jsfiddle.net/hy1d84jL/ jsfiddle: https ://jsfiddle.net/hy1d84jL/

EDIT: You can think of recursion as an infinite for..loop . 编辑:您可以将递归视为for..loop的无限。 Let's just use it in "controlled" way and define the bounds - 0 for minimum and the length of Obama word as the maximum. 让我们以“受控”方式使用它,并定义界限-最小为0, Obama单词的长度为最大。 Now, let's just make it call itself whatever number of times and do what you need in order to reverse the string, which is - decrement the index variable by one and sum the character from the end. 现在,让我们自己调用任意次数,并执行所需的操作以反转字符串,这是-将index变量减1,然后从最后算起字符。 Hope it helps. 希望能帮助到你。 Nice question. 好问题。

If the function can only have the single input i would split the string into smaller and smaller pieces, and add them all together in the reverse order 如果函数只能有一个输入,我将把字符串分成越来越小的部分,并以相反的顺序将它们全部加在一起

function reverseString(strToReverse){
  if (strToReverse.length <= 1) { return strToReverse; }

  return reverseString(strToReverse.substr(1, strToReverse.length - 1) + strToReverse[0];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM