C实现 LeetCode->Reverse Integer

来源:互联网 发布:广联达预算软件手机版 编辑:程序博客网 时间:2024/04/29 21:06

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321



反转整形


主要是注意边界条件

1:   如果整数的最后一位为0,应该输出什么?例如,如100。
  
  
2: 逆转整数可能溢出;假设输入是一个32位整数,然后反向1000000003溢出
  


////  PalindromeNumber.c//  Algorithms////  Created by TTc on 15/6/6.//  Copyright (c) 2015年 TTc. All rights reserved.//#include "PalindromeNumber.h"#include <ctype.h>#include <limits.h>#include <stdlib.h>#include <stdbool.h>#include <string.h>staticint reverse(int x) {    long long val = 0;    do    {        val = val * 10 + x % 10;        x /= 10;    } while (x);        return (val > INT_MAX || val < INT_MIN) ? 0 : val;}/*


0 0
原创粉丝点击