Leecode刷题之旅-C语言/python-7.整数反转
/*
* @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.整数反转的更多相关文章
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
- Leecode刷题之旅-C语言/python-9.回文数
/* * @lc app=leetcode.cn id=9 lang=c * * [9] 回文数 * * https://leetcode-cn.com/problems/palindrome-num ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
随机推荐
- AOP的实现
AOP基于xml配置方式实现 Spring基于xml开发AOP 定义目标类(接口及实现类) /** * 目标类 */ public interface UserService { //业务方法 pub ...
- May 17th 2017 Week 20th Wednesday
Men are nearly always willing to believe what they wish. 人总爱想入非非,把愿望变成现实. It is just the humancondit ...
- dos基础+环境搭建基础理论
dos基础 市面上两大操作系统 windows.*nix(unix.linux.mac.bsd(安全性比较高)) 后三种都属于unix的衍生版本 linux是为了兼容unix开发的,最后开放了源代码 ...
- 如何查找Authorization object在哪些ABAP代码里使用到
使用事务码SUIM: 双击where-Used List->Authorization Objects->In Programs: 输入要查找的Authorization Object名称 ...
- 设计模式——装饰模式(Decorator Pattern)
装饰模式:动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活. UML图: 模型类: Component类: package com.cnblog.clarck; /** ...
- Android(java)学习笔记3:线程的优先级
1. Java线程的优先级从1到10级别,值越大优先级越高线程默认优先级是5.值越大优先级越高 (1) 继承自Thread类创建线程类: package cn.itcast_04; public cl ...
- HDU 1521 指数型母函数
方法一: DFS 方法二:生成函数 每个数可以重复一定次数,求排列组合数,这是裸的指数型生成函数: #include <bits/stdc++.h> using namespace std ...
- DateTime小综合
实现效果: 关键知识: 1>DateTime类的ToString()方法: 2>DateTime类的IsLeapYear(); 3>DateTime类的DaysInMomth(); ...
- 【luogu P3371 单源最短路径】 模板 SPFA
题目链接:https://www.luogu.org/problemnew/show/P3371 我永远都喜欢Flyod.dijkstra + heap.SPFA #include <cstdi ...
- LeetCode14.最长公共前缀 JavaScript
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...