/*
* @lc app=leetcode.cn id=7 lang=c
*
* [7] 整数反转
*
* https://leetcode-cn.com/problems/reverse-integer/description/
*
* algorithms
* Easy (31.36%)
* Total Accepted: 77.7K
* Total Submissions: 247.8K
* Testcase Example: '123'
*
* 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
*
* 示例 1:
*
* 输入: 123
* 输出: 321
*
*
* 示例 2:
*
* 输入: -123
* 输出: -321
*
*
* 示例 3:
*
* 输入: 120
* 输出: 21
*
*
* 注意:
*
* 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231,  231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
*
*/
int reverse(int x) {
long i =;
long t = x;
while(t){
i = i*+(t%);
t= t/;
}
if (i < INT_MIN || i >INT_MAX) //判定是否在int可表达的有效范围内
{
return ;
}
return i;
}

这道题相对来说很好理解,用余数除10的方法就可以实现整数的翻转。

要注意,这里设置成long类型,然后在最后判断是否在int范围内。否则会超出范围。

-----------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=7 lang=python3
#
# [7] 整数反转
#
# https://leetcode-cn.com/problems/reverse-integer/description/
#
# algorithms
# Easy (31.70%)
# Total Accepted: 86K
# Total Submissions: 271.4K
# Testcase Example: '123'
#
# 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
#
# 示例 1:
#
# 输入: 123
# 输出: 321
#
#
# 示例 2:
#
# 输入: -123
# 输出: -321
#
#
# 示例 3:
#
# 输入: 120
# 输出: 21
#
#
# 注意:
#
# 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−2^31,  2^31 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
#
#
class Solution:
def reverse(self, x: int) -> int:
plus_minus = ""
reverse_x = ""
if x<0:
plus_minus = "-"
x = -x
for i in str(x):
reverse_x = i + reverse_x
reverse_x = plus_minus +reverse_x
if int(reverse_x)>pow(2,31)-1 or int(reverse_x)<pow(-2,31):
return 0
return int(reverse_x)

python这里得益于高级脚本语言的便捷,可以先把整形转成字符串,按后一位+前一位 这样的方式就可以实现翻转。

然后再把字符串转换成int类型(在这之前要判断其范围)

Leecode刷题之旅-C语言/python-7.整数反转的更多相关文章

  1. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

  2. Leecode刷题之旅-C语言/python-9.回文数

    /* * @lc app=leetcode.cn id=9 lang=c * * [9] 回文数 * * https://leetcode-cn.com/problems/palindrome-num ...

  3. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  4. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  5. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  6. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  7. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  8. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  9. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

随机推荐

  1. python加解密

    from Crypto.Cipher import AES import base64 secret_key = '1234567890123456' # 密匙 msg_text = 'test so ...

  2. Android端访问服务器核心代码

  3. 【Leetcode】【Easy】Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  4. Orchard Core 文档翻译 (三) Orchard Core Modules

    原文连接:https://www.cnblogs.com/Qbit/p/9746442.html 转载请注明出处 介绍 Orchard Core Modules库提供了一种机制,可以拥有一个独立的模块 ...

  5. Ajax 重构的步骤

    Ajax重构大致可以分为以下3三个步骤. 一 创建一个单独的JS文件,名称为AjaxRequest.js,并且在该文件中编写重构Ajax 所需的代码具体代码如下:var net = new Objec ...

  6. 设计模式——模板方法模式(TemplateMethod Pattern)

    模板方法模式:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. UML图: 抽象模板: package com.cnblo ...

  7. POJ-3267 The Cow Lexicon---删除字符匹配单词

    题目链接: https://cn.vjudge.net/problem/POJ-3267 题目大意: 题意就是给出一个主串,和一本字典,问最少在主串删除多少字母,可以使其匹配到字典的单词序列. PS: ...

  8. Android(java)学习笔记43:Map集合的遍历之键找值

    1. Map集合的遍历之键找值  package cn.itcast_01; import java.util.HashMap; import java.util.Map; import java.u ...

  9. css实现单行的靠左靠右和居中效果

    1.父元素    text-align:center 2.子元素 .left{ float:left; } .right{ float:right; } .center{ display:inline ...

  10. DEM、DTM和DSM的区别

    一.DTM(Digital Terrain Model) 数字地面模型是利用一个任意坐标系中大量选择的已知x.y.z的坐标点对连续地面的一个简单的统计表示,或者说,DTM就是地形表面形态属性信息的数字 ...