LeetCode 7. Reverse Integer(c语言版)
题目:
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
题解:
因为还不太会用c++和java,所以用c语言写的,题意很好理解,题也很简单,但需要注意溢出的问题,输入没有溢出,但翻转可能有溢出。
代码:
int reverse(int x) {
long long y=; //用longlong为了看是否有溢出
if(x>=pow(,)-||x<=-pow(,)) return ;
while(x!=)
{
int v=x%;
y=y*+v;
if(y>pow(,)-||y<-pow(,)) return ;
x/=;
}
int n=y;
return n;
}
LeetCode 7. Reverse Integer(c语言版)的更多相关文章
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
- leetcode:Reverse Integer【Python版】
1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- LeetCode 7. Reverse Integer (倒转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
随机推荐
- Django(八)上:Model操作和Form操作
↑↑↑点上面的”+”号展开目录 Model和Form以及ModelForm简介 Model操作: 创建数据库表结构 操作数据库表 做一部分的验证 Form操作: 数据验证(强大) ModelForm ...
- MySQL 5.6 for Windows 解压缩版配置安装 和 MySQL 数据库的安装和密码的设定
https://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html https://jingyan.baidu.com/article/09 ...
- javascript深入浅出——学习笔记(包装对象和类型检测)
3包装对象:https://www.imooc.com/video/5676 当我们尝试把基本类型已对象的方式访问时,javascript会把该基本类型转换为对应的包装类型对象(临时对象),相当于ne ...
- webview 更新方法细节
plus.webview.getWebviewById("user.home").evalJS("getHomeData()");//调用页面方法 plus.w ...
- 【MySQL 读书笔记】全局锁 | 表锁 | 行锁
全局锁 全局锁是针对数据库实例的直接加锁,MySQL 提供了一个加全局锁的方法, Flush tables with read lock 可以使用锁将整个表的增删改操作都锁上其中包括 ddl 语句,只 ...
- Consecutive Sum LightOJ - 1269(区间异或和)
Consecutive Sum 又来水一发blog... 本来是昨天补codechef的题,最后一道题是可持久化字典树,然后去黄学长博客看了看 觉得字典树写法有点不太一样,就想着用黄学长的板子写码几道 ...
- Error:Failed to resolve: com.android.support.constraint:constraint-layout:1.0.2
可以换个maven库: allprojects { repositories { jcenter() //maven { url "https://jitpack.io" } ma ...
- 使用linux backtrace打印出错函数堆栈信息
一般察看函数运行时堆栈的方法是使用GDB(bt命令)之类的外部调试器,但是,有些时候为了分析程序的BUG,(主要针对长时间运行程序的分析),在程序出错时打印出函数的调用堆栈是非常有用的. 在glibc ...
- bean属性复制到另外一个bean
import org.springframework.beans.BeanUtils; BeanUtils.copyProperties(maker.getBaseInfo(), newBasInfo ...
- java返回json设置自定义的格式
使用注解@JsonSerialize(using = CustomPriceSerialize.class) 创建自定义的格式化类(可为内部类) /** * 设置默认返回的小数类型(0.01 元) * ...