zoukankan      html  css  js  c++  java
  • LeetCode 7

    Reverse Integer

    Reverse digits of an integer.

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

     1 /*************************************************************************
     2     > File Name: LeetCode007.c
     3     > Author: Juntaran 
     4     > Mail: Jacinthmail@gmail.com
     5     > Created Time: 2016年04月24日 星期日 17时03分19秒
     6  ************************************************************************/
     7 
     8 /*************************************************************************
     9 
    10     Reverse Integer
    11     
    12     Reverse digits of an integer.
    13 
    14     Example1: x = 123, return 321
    15     Example2: x = -123, return -321
    16 
    17  ************************************************************************/
    18  
    19 #include <stdio.h>
    20 #include <limits.h>
    21 
    22 int reverse(int x){
    23 
    24     long int result = 0;
    25 
    26     /* 取出数字 */
    27     int p = x>0 ? x : -x;
    28     
    29     /* Reverse */
    30     while( p != 0 ){
    31         result = result*10 + p%10;
    32         p = p/10;
    33     }
    34     
    35     /* 添加符号 */
    36     result = (x>0) ? result : -result;
    37     
    38     /* 判断溢出 */
    39     if( result > INT_MAX || result < INT_MIN ){
    40         return 0;
    41     }else{
    42         printf("%d
    ",result);
    43         return result;
    44     }
    45 }
    46 
    47 int reverse1(int x){
    48     
    49     long result = 0;
    50     while(x != 0){
    51         result = result * 10 + x % 10;
    52         x = x/10;
    53     }
    54     if( result > INT_MAX || result < INT_MIN ){
    55         return 0;
    56     }
    57     printf("%d
    ",result);
    58     return (int)result;
    59 }
    60 
    61 int main(){
    62     int x = -15984;
    63     reverse(x);
    64     reverse1(x);
    65 }
  • 相关阅读:
    WPF Timer替代者
    <转>Change the Background of a selected ListBox Item
    WPF Button样式模板
    WPF中自定义只能输入数字的TextBox
    ansible playbook模式及语法
    数据挖掘Kaggle
    电影网站
    数据挖掘面临的科学和工程的新问题
    KDD Cup 2012(今年数据挖掘在中国)
    能力是在执行中实现的,要高节奏不要详细的设计
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5428764.html
Copyright © 2011-2022 走看看