leetcode 7 Reverse Integer(水题)
so easy,注意一下输入不爆int但是反转以后可能爆int。
class Solution {
public:
int gao(int w){
if(w==) return ;
else{
int ans=;
while(w--){
ans*=;
}
return ans;
}
}
int reverse(int x) {
vector<int>v;
if(x>=){
int t=x;
while(t>){
v.push_back(t%);
t/=;
}
long long int ans=;
int siz=v.size();
int st=siz-;
for(int i=;i<siz;i++){
ans+=(long long int)v[i]*gao(st);
st--;
}
if(ans<)
return (int)ans;
else return ;
}
else{
x=-x;
int t=x;
while(t>){
v.push_back(t%);
t/=;
}
long long int ans=;
int siz=v.size();
int st=siz-;
for(int i=;i<siz;i++){
ans+=(long long int)v[i]*gao(st);
st--;
}
if(ans<=)
return (int)-ans;
else return ;
}
}
};
leetcode 7 Reverse Integer(水题)的更多相关文章
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
- [leetcode] 7. Reverse Integer (easy)
原题 水题 唯一注意的点就是数字溢出 class Solution { public: int reverse(int x) { long long MAX = ((long long)1 <& ...
- 【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 ...
- Leetcode 7 Reverse Integer 数论
题意:将整数倒置,该题简单但是需要注意数据的范围,难得的好题. 如果出现1000000003或者-2000000003,倒置后的数超过int的范围,因此返回0,出现这种情况可以使用long long, ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
随机推荐
- 微信小程序的官方文档
虽然不知道微信小程序今后的发展情况,不过做为一名it人员的我还是去了解它. 这是他的文档路径,里面有详细的使用和申请内测号的全部流程,这里就不再过多解释了. 看后那个开发小程序的文档记得分析你感觉微信 ...
- SQL SERVER 查看表是否存在
查看表是否存在 if exists(select 1 from sysobjects where id = OBJECT_ID('数据库名称.dbo.表明称')) drop table 为字段添加注释 ...
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its 错误解决办法
This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary log ...
- POJ 1694 An Old Stone Game【递归+排序】
链接: http://poj.org/problem?id=1694 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...
- 学院名单-211院校研招学院-中国教育在线(www.eol.cn)170915164402
[数据结果] 学校数.学院数:112,2657. [数据来源] 中国教育在线(www.eol.cn)211院校研招学院. http://www.eol.cn/html/ky/gxmd/211.shtm ...
- 数据处理 数据入数据库 与 Excel
Python 数据处理 中间数据 Excel 团队交流分工 低的沟通成本 数据入数据库 如postgresql
- vue 计算属性和监听器
一.计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div> {{ message.split('').rev ...
- 13.Django模版
没什么好说的,看官方文档 https://docs.djangoproject.com/en/1.9/ref/templates/builtins/
- Linux改动hostname的两个办法
假设你想把主机名改为 linux的话.两中方法: 1. # hostname linux 这样改动了以后马上生效.可是重新启动后就没了 2. # vi /etc/sysconfig/network 改 ...
- Tensorflow官方文档中文版——第一章
第一示例: import tensorflow as tf import numpy as np x_data=np.float32(np.random.rand(,))#随机输入 y_data=np ...