Reverse Integer

Reverse Integer

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

全栈程序员社区此处内容已经被作者隐藏,请输入验证码查看内容
验证码:
请关注本站微信公众号,回复“验证码”,获取验证码。在微信里搜索“全栈程序员社区”或者“www_javaforall_cn”或者微信扫描右侧二维码都可以关注本站微信公众号。

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

Have you thought about this?

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.

非常easy的一道题。

例如以下:

public class Solution {    public int reverse(int x) {      if(x == 0){          return 0;      }      int fuhao = x >> 31 & 1;//1为负数      if(fuhao == 1){          x = -x;//转换成正数       }      StringBuffer sb = new StringBuffer();      while(x > 0){          sb.append(x%10);          x /= 10;      }      try{        x = Integer.parseInt(sb.toString());      }catch(Exception ex){          return 0;//转换之后的数据溢出      }      if(fuhao == 1){          x = -x;      }      return x;    }}

Runtime: 256 ms

注意题后的特殊情况,一个正常的数转换后可能出错,如input为1534236469,则retrun 0;

參考别人的代码,发现自己的思维固定在了Sstring上了,改进例如以下:

public class Solution {    public int reverse(int x) {      if(x == 0){          return 0;      }      int fuhao = x >> 31 & 1;//1为负数      if(fuhao == 1){          x = -x;//转换成正数       }      long x1 = 0;      while(x > 0){          x1 = x1 * 10 + x%10;          x /= 10;      }      if(x1 > Integer.MAX_VALUE || x1 < Integer.MIN_VALUE){//转换失败          return 0;      }      if(fuhao == 1){          x = -(int)x1;      }else{          x = (int)x1;      }      return x;    }}

Runtime:
217 ms

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117959.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • NLTK FreqDist

    NLTK FreqDistFreqDisknltkFreqDisk函数能够统计数组当中单词出现的次数。text=[‘hadoop’,’spark’,’hive’,’hadoop’,’hadoop’,’spark’,’lucene’,’hadoop’,’spark’,’hive’,’hadoop’,’hadoop’,’spark’,’pig’,’zookeeper’,’flume’,’…

    2022年10月30日
  • 深度学习图像标注工具汇总

    深度学习图像标注工具汇总对于监督学习算法而言,数据决定了任务的上限,而算法只是在不断逼近这个上限。世界上最遥远的距离就是我们用同一个模型,但是却有不同的任务。

    2022年6月1日
  • maven repositories配置_maven排除依赖

    maven repositories配置_maven排除依赖eclipsemaven配置修改:mavenrepository配置http://blog.csdn.net/joewolf/article/details/4876604Maven缺省

    2022年8月3日
  • 极智开发 | ubuntu源码编译opencv

    极智开发 | ubuntu源码编译opencv本教程详细记录了在ubuntu1804中源码编译opencv的方法。

    2022年7月20日
  • C++学习之路—— C++异常处理机制

    C++学习之路—— C++异常处理机制例题:1.设计一个程序,根据输入的三边求三角形面积。2.要求设置异常处理机制,对不符合三角形条件提示错误信息,不做计算;对输入非正数的边提示错误,并重新输入。代码如下:class S{public: double s; S(double x, double y, double z) { a = x; b = y; c = z; } void TryS();pr…

    2022年8月18日
  • SQLite数据库中文乱码处理「建议收藏」

    SQLite数据库中文乱码处理「建议收藏」通过SQLiteAdministrator等工具生成的数据库文件,放入到Android中,查询记录怎么也查不出来,后来发现是编码错误,SQLiteAdministrator不支持utf编码,所以存储的中文会出现乱码的情况,处理方法如下:SQLiteDatabasedb=dbHelper.getWritableDatabase();Cursorcursor

    2022年9月3日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号