Created
September 11, 2020 13:21
-
-
Save 2hanX/410cc7bf54fc087cf46dd2a1198c3b89 to your computer and use it in GitHub Desktop.
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1: 输入: 123
输出: 321 示例 2: 输入: -123
输出: -321 示例 3: 输入: 120
输出: 21 来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reverse-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<limits.h> | |
int main(int argc, char const *argv[]) | |
{ | |
int x = -123; | |
int ans = 0; | |
while(x != 0) | |
{ | |
int pop = x % 10; | |
if (ans > INT_MAX / 10 || (ans == INT_MAX / 10 && pop > 7)) | |
{ | |
return 0; | |
} | |
if (ans < INT_MIN / 10 || (ans == INT_MIN / 10 && pop <-8)) | |
{ | |
return 0; | |
} | |
ans = ans * 10 + pop; | |
x/=10; | |
} | |
printf("%d\n", ans); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment