하루의 쉼터

[LeetCode] 7. Reverse Integer 본문

Coding Test/LeetCode

[LeetCode] 7. Reverse Integer

Changun An 2021. 10. 5. 09:16
반응형

Question :
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 [-231, 231 - 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:

Example 2:
Input: x = -123
Output: -321
Example 3:

Example 3:
Input: x = 120
Output: 21
Example 4:

Example 4:
Input: x = 0
Output: 0

 

Constraints:
-231 <= x <= 231 - 1

Start Class:

class cl_solution{
public :
    int reverse(int x) {

    }
};

 

전체 FlowCharts: 

 

cl_solution.h

#include <iostream>
#include <cmath>
class cl_solution{
public :
	
	int reverse(int x);
	bool fn_data_check(int x);
	int fn_user_input();
	int fn_run();
};

 

cl_solution.h

#include "cl_solution.h"
int cl_solution::reverse(int x) {
	
    long result = 0;
    bool flag = true;
    if (!fn_data_check(x))
        return 0;


    flag = (x >= 0) ? true : false;


    if (!flag) {
        x *= -1;
    }

    while (x >= 10) {
        result += x % 10;
        result *= 10;
        x /= 10;
    }
    result += x % 10;


    if (!flag)
        result *= -1;


    return fn_data_check(result) ? result : 0;
	
	
} 

bool cl_solution::fn_data_check(int x) {
	return  (x <= -2147483648 || x >= 2147483647) ? false : true;
}


int cl_solution::fn_user_input() {
	int temp_data = 0;
	std::cout << "Plz User Data" << std::endl;
	std::cin >> temp_data;
	return fn_data_check(temp_data) ? temp_data : fn_user_input();
}

int cl_solution::fn_run() {
	std::cout << reverse(fn_user_input()) << std::endl;
	
	return 0;
}

 

코드 분석:

1. 유저 데이터 입력

int cl_solution::fn_user_input() {
	int temp_data = 0;
	std::cout << "Plz User Data" << std::endl;
	std::cin >> temp_data;
	return fn_data_check(temp_data) ? temp_data : fn_user_input();
}

 

2. 데이터 체크

* 처음에는 std::pow를 이용하였으나, 홈페이지 제출하며 수정하였음.

bool cl_solution::fn_data_check(int x) {
	return  (x <= -2147483648 || x >= 2147483647) ? false : true;
}

 

3. 데이터 음수, 양수 체크

* 데이터 계산 및 결과 반영을 위하여 음수 체크

    flag = (x >= 0) ? true : false;

    if (!flag) {
        x *= -1;
    }

4. 데이터 처리

    while (x >= 10) {
        result += x % 10;
        result *= 10;
        x /= 10;
    }
    result += x % 10

5. 음수 및 범위 체크

    if (!flag)
        result *= -1;

    return fn_data_check(result) ? result : 0;

Result : 

Github :

https://github.com/Anchangun/LeetCode/tree/main/Cpp/7.%20Reverse%20Integer

 

GitHub - Anchangun/LeetCode: 문제풀이

문제풀이. Contribute to Anchangun/LeetCode development by creating an account on GitHub.

github.com

 

Leetcode:

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

 

Reverse Integer - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

반응형

'Coding Test > LeetCode' 카테고리의 다른 글

[LeetCode] 9. Palindrome Number  (0) 2021.10.07
[LeetCode] 206. Reverse Linked List  (0) 2020.12.06
[LeetCode] 53. Maximum Subarray  (0) 2020.11.30
[LeetCode] 21. Merge Two Sorted Lists  (0) 2020.11.24
[LeetCode] 136. Single Number  (0) 2020.11.23
Comments