Leetcode: Reverse Integer 正确的思路下-要考虑代码简化
题目:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
我是这样写的:
class Solution {
public:
int reverse(int x) {
// int temp = INT_MAX;
int count = ;
int y = x;
while(abs(y) > )
{
y = y/;
count++;
}
if(x == )
return ;
else if(x > )
{
int *a = new int[count];
for(int i = ; i < count; i++)
{
a[i] = x%;
x = x/;
}
int value = ;
int temp = ;
for(int j = ; j < count; j++)
{
value = value* + a[j];
if(temp != (value - a[j])/) value = temp = ;
else temp = value;
}
delete []a;
return value;
}
else
{
x = -x;
int *a = new int[count];
for(int i = ; i < count; i++)
{
a[i] = x%;
x = x/;
}
int value = ;
int temp = ;
for(int j = ; j < count; j++)
{
value = value* + a[j];
if(temp != (value - a[j])/) value = temp = ;
else temp = value;
}
delete []a;
return -value; } }
};
通过之后,我参考了网友doc_sgl的代码
http://blog.csdn.net/doc_sgl/article/details/12190507
他是这样写的:
int reverse(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function long res = ;
while(x)
{
res = res* + x%;
x /= ;
}
return res;
}
十几行与几十行的区别,却能更简便的表述。虽然这些代码没有仔细思量裁剪,但却表现了代码能力的区别。以此自省之。
Leetcode: Reverse Integer 正确的思路下-要考虑代码简化的更多相关文章
- LeetCode: Reverse Integer 解题报告
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- C++ leetcode::Reverse Integer
第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的.深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多.最近丧的不行,不管怎么 ...
- leetcode:Reverse Integer【Python版】
1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...
- LeetCode——Reverse Integer(逆置一个整数)
问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321 Ha ...
- LeetCode——Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- [Leetcode]Reverse Integer
核心思想:原数对10取余数赋值给新数后降一位,再把新数升一位加上下一次原数取余值,直到原数降为0. 解法如下: int reverse(int x) { bool minus = false; ) ...
- leetcode reverse integer&&Palindrome Number
public class Solution { public int reverse(int x) { int ret=0; while(x!=0) { int t=x%10; ret=ret*10+ ...
随机推荐
- hdoj 1969 Pie【二分】
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 什么是VPN?
VPN----虚拟专用网络 虚拟专用网络的功能:在公用网络上建立专用网络,进行加密通讯.在企业网络汇总有广泛应用.vpn网关通过对数据包的加密和数据包目标地址的转换事项远程访问.vpn有多种分类方式, ...
- 用code workshop取代code review
Box Tech Blog » Effective learning through code workshops介绍了Box如何用code workshop而不是code review的形式来改善代 ...
- Imatest 崩溃
在使用Imatest时候,发现选取chart图的区域时候.Imatest停止工作,例如以下图.百度半天没有找到类似的问题,事实上这个问题在之前的公司也碰到过一回,卸载重N次,无效.如今又碰到了,问之前 ...
- [Angular + Unit] AngularJS Unit testing using Karma
http://social.technet.microsoft.com/wiki/contents/articles/32300.angularjs-unit-testing-using-karma- ...
- Mysql新建用户和数据库并授权
测试环境:Centos 6.3和Mysql 5.3 一.新建用户 //登录MYSQL@>mysql -u root -p@>密码//创建用户mysql> insert into my ...
- Annotation Type @bean,@Import,@configuration使用--官方文档
@Target(value={METHOD,ANNOTATION_TYPE}) @Retention(value=RUNTIME) @Documented public @interface Bean ...
- tomcat work 目录
用tomcat作web服务器的时候,部署的程序在webApps下,这些程序都是编译后的程序(发布到tomcat的项目里含的类,会被编译成.class后才发布过来,源文件没有发布过来,但这里的jsp没有 ...
- Java基础知识强化之集合框架笔记32:集合之可变参数的概述和使用
1. 可变参数的概述和使用: (1)可变参数:定义方法的时候不知道该定义多少个参数(2)格式: 修饰符 返回值类型 方法名(数据类型… 变量名){ } 注意: 这里的变量其实是一个数 ...
- CSS3 box-sizing 属性
定义和用法 box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. 例如,假如您需要并排放置两个带边框的框,可通过将 box-sizing 设置为 "border-box& ...