Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

My Solution:

public class Solution {
    public string ReverseString(string s) {

        if (string.IsNullOrEmpty(s)) {
            return s;
        }

        char[] resChar = s.ToCharArray();
        int s_length = s.Length;

        for (int i = 0; i<s_length/2; i++) {
            char temp = resChar[i];
            resChar[i] = resChar[s_length-i-1];
            resChar[s_length-i-1] = temp;
        }

        string res = new string(resChar);

        return res;



    }
}

Time Complexity: O(N) O(N)

results matching ""

    No results matching ""