online advertising

Saturday, May 21, 2016

LeetCode OJ - Reverse String

Problem:

Please find the problem here.

Analysis:

Another low hanging fruit - how hard can it be to reverse a string?

Solution:

Create a new buffer to store the string, fill the buffer in reversed order. The code made one extra copy of the buffer in order to fit the required interface.

Code:

#include "stdafx.h"

// https://leetcode.com/problems/reverse-string/

#include "LEET_REVERSE_STRING.h"
#include <map>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

namespace _LEET_REVERSE_STRING
{
    class Solution
    {
    public:
        string reverseString(string s)
        {
            size_t length = s.length();
            char* y = new char[length + 1];
            for (size_t i = 0; i < length; i++)
            {
                y[length - i - 1] = s[i];
            }
            y[length] = '\0';
            string result = y;
            delete[] y;
            return result;
        }
    };
};

using namespace _LEET_REVERSE_STRING;

int LEET_REVERSE_STRING()
{
    Solution solution;
    cout << solution.reverseString("Hello") << endl;
    return 0;
}

No comments :

Post a Comment