0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Leetcode: Reverse String

Posted at

require 'minitest/autorun'
require 'pry'
class ReverseString < Minitest::Test

  def test_run
    input = ["h", "e", "l", "l", "o"]
    output = ["o", "l", "l", "e", "h"]

    assert_equal(output,
                 reverse_string(input))
  end

  def reverse_string(s, index = 0)
    return s if index == s.size / 2

    first_char = s[index]
    last_char = s[s.size - 1 - index]
    s[index] = last_char
    s[s.size - 1 - index] = first_char
    index += 1
    reverse_string(s, index)
    s
  end

end
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?