Solution in C#


  • 0
    K

    public class Solution {
    public int Reverse(int x)
    {
    bool ison = false;
    string str = x.ToString();
    char[] numArr = str.ToCharArray();
    List<char> temp = new List<char>();
    for (int i = 0; i < numArr.Length; i++)
    {
    temp.Add(numArr[i]);
    }
    if (temp[temp.Count - 1] == '0')
    {
    temp.RemoveAt(temp.Count - 1);
    }
    char[] arr = new char[temp.Count];
    string num = string.Empty;
    for (int i = 0; i < temp.Count; i++)
    {
    if (temp[i] == '-')
    {
    arr[i] = temp[i];
    num += arr[i];
    ison = true;
    continue;
    }
    if (ison)
    {
    arr[i] = temp[temp.Count - i];
    num += arr[i];
    }
    else
    {
    arr[i] = temp[temp.Count - i - 1];
    num += arr[i];
    }
    }
    return int.Parse(num) ;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.