Problem solving/Algorithms

[LeetCode] 7. Reverse Integer (C#)

Young_A 2021. 1. 23. 11:21

๋ชฉ์ฐจ

    LeetCode - Problems - Algorithms - 7.Reverse Integer

    Problem Description

    Given a signed 32-bit integer x, return x with its digits reversed.

    If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], then return 0.

    Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

     

    Example 1:

    • Input: x = 123
    • Output: 321

    Example 2:

    • Input: x = -123
    • Output: -321

    Example 3:

    • Input: x = 120
    • Output: 21

    Example 4:

    • Input: x = 0
    • Output: 0

    Constraints:

    • -231 <= x <= 231 - 1

    My Solution (C#)

    public class Solution {
        public int Reverse(int x) {
            string num;
            string reversed;
            if (x < 0)
            {
                num = Convert.ToString(x * (-1));
                reversed = "-";
            }
            else
            {
                num = Convert.ToString(x);
                reversed = "";
            }
            for (int i = num.Length - 1; i >= 0; i--)
            {
                reversed += num.Substring(i, 1);
            }
            try
            {
                return Convert.ToInt32(reversed);
            }
            catch
            {
                return 0;
            }
        }
    }

    ์ฒซ๋ฒˆ์งธ if ๋ฌธ: ์ฃผ์–ด์ง„ int๊ฐ’์„ string ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

    (์Œ์ˆ˜์ผ ๊ฒฝ์šฐ -๋ฅผ reversed์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ฃผ์–ด์ง„ ์ˆซ์ž์— -1์„ ๊ณฑํ•œ ๋’ค ๋ณ€ํ™˜ํ•œ๋‹ค.)

     

    for ๋ฐ˜๋ณต๋ฌธ: ๋ณ€ํ™˜๋œ string ๊ฐ’์˜ ์ œ์ผ ๋’ท ๊ฐ’์„ reversed์— ์ถ”๊ฐ€ํ•œ๋‹ค.

     

    ๊ทธ๋ ‡๊ฒŒ ๋’ค์ง‘์€ ๊ฐ’์„ int ๊ฐ’์œผ๋กœ ์žฌ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฆฌํ„ดํ•œ๋‹ค.

    ์ด๋•Œ int์˜ ๋ฒ”์œ„๋ฅผ ์ดˆ๊ณผํ•  ๊ฒฝ์šฐ 0์„ ๋ฆฌํ„ดํ•˜๋ผ๊ณ  ํ–ˆ์œผ๋‹ˆ try catch๋ฌธ์„ ์ด์šฉํ•ด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ 0์„ returnํ•˜๋„๋ก ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•œ๋‹ค.

    7. Reverse Integer - Used string Conversion.

    ์•„๋ž˜๋Š” ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋ฐฐ์šฐ์ง€ ๋ชปํ•œ 1๋…„1๊ฐœ์›” ์ „์˜ ๋‚ด๊ฐ€ ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ.

    ๋”๋ณด๊ธฐ
    public class Solution {
        public int Reverse(int x) {
            long result = 0;
            while (x != 0)
            {
                result = result * 10 + x % 10;
                x /= 10;
            }
            return (int)result == result ? (int)result : 0;
        }
    }

     

    int ๊ฐ’์„ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผํ• ์ง€ ๋ชฐ๋ผ์„œ ๊ทธ๋ƒฅ long ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.

    ๊ทธ๋ ‡์ง€๋งŒ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜์ง€ ๋ชปํ•˜๋ฏ€๋กœ ์ •๋‹ต์ด ๋  ์ˆ˜ ์—†๋‹ค. (Leetcode์—์„œ๋Š” ์ •๋‹ต์ฒ˜๋ฆฌ๋ฅผ ํ–ˆ๋‹ค.)

    LeetCode - 7. Reverse Integer - Wrong answer

    'Problem solving > Algorithms' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    [LeetCode] 13. Roman to Integer (C#)  (0) 2021.01.24
    [LeetCode] 9. Palindrome Number (C#)  (0) 2021.01.23
    [LeetCode] 1. Two Sum (C#)  (0) 2021.01.21
    [LeetCode] 1646. Get Maximum in Generated Array (Python)  (0) 2021.01.16
    [LeetCode]Daily Challenge: Boats to Save People (Python)  (0) 2021.01.13