zoukankan      html  css  js  c++  java
  • [LeetCode] 7. Reverse Integer

    #include <stdio.h>
    #include <math.h>
    
    // 7. Reverse Integer
    // Difficulty: Easy
    // Reverse digits of an integer.
    // Example1: x = 123, return 321
    // Example2: x = -123, return -321
    // https://leetcode.com/problems/reverse-integer/
    
    int reverse(int x) {
    
        // *** METHOD 2
        long y = 0;            // a trick, may be improved
        while (x) {
            y = y * 10 + x % 10;
            x /= 10;
            if (y > pow(2,31) - 1 || y < -1 * pow(2,31)) {
                printf("(2^31) - 1 = %d, -1 * (2^31) = %d, y = %ld
    ", (int)pow(2,31) - 1, -1 * (int)pow(2,31), y);
                return 0;    // IMPORTANT: consider range of int
            }
        }
        return y;
    }
    
    int main() {
        int x = 1534236469;
        printf("x = %d, reversed x = %d
    ", x, reverse(x));
        return 0;
    }
  • 相关阅读:
    DOM
    js中字符串常规操作
    placeholer改变默认灰色
    css重置reset.css
    倒计时跳转
    手机中间四位用*代替
    animation
    过渡
    flex布局
    css3几个新属性
  • 原文地址:https://www.cnblogs.com/skyssky/p/5740375.html
Copyright © 2011-2022 走看看