코딩테스트 준비/leetcode

7. Reverse Integer [Easy]

MAKGA 2021. 6. 14. 17:06
320x100

출처: 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

 

주어진 정수를 반대로 출력하면 된다.

ex 123 -> 321.

 

class Solution { 
public: 
	int reverse(int x) { 
		if (0 == x) { 
			return 0; 
		} 

		int64_t ret = 0; 
		while (0 != x) 
		{
			int64_t temp = (ret * 10) + (x % 10); 
			ret = (ret * 10) + (x % 10); 
			x /= 10; 
		} 

		return (int)ret == ret ? ret : 0; 
	}
};

 

 

계산식에서 오버플로 나서 그거때문에 Runtime Error 세번..

320x100