题目描述
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
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?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

解析:看了题目描述了这么多废话,无非是输入10返回1,输入-21返回-12等等。解析思路就是该怎么来就怎么来,翻转数字,注意负号的判断就好了。

public class Solution {public int reverse(int x) {if(x == 0){return x;}while (x%10==0){x/=10;}String s=String.valueOf(x);boolean flag=false;StringBuilder sb = new StringBuilder();for(int i=s.length()-1;i>=0;i--){if(i==0){if(s.charAt(i)=='-'){flag=true;continue;}}sb.append(s.charAt(i));}return flag==true?-1*Integer.parseInt(sb.toString()):Integer.parseInt(sb.toString());}
}

《leetcode》reverse-integer相关推荐

  1. 【翻转整数考虑溢出】LeetCode 7. Reverse Integer

    LeetCode 7. Reverse Integer Solution1:最笨的方法 class Solution {public:int reverse(int x) {if (!x) retur ...

  2. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  3. 《LeetCode》数据结构入门板块

    文章目录 <LeetCode题>数据结构入门板块 第1天 数组 217.存在重复元素[简单,哈希表] 53.最大子序和[简单,动态规划,贪心] 第2天 数组 1.两数之和[简单,哈希表] ...

  4. LeetCode - 7. Reverse Integer

    7. Reverse Integer Problem's Link ------------------------------------------------------------------ ...

  5. LeetCode之Reverse Integer

    1.题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 cli ...

  6. LeetCode - 7 - Reverse Integer

    题目 URL:https://leetcode.com/problems/reverse-integer 解法 这个题目是极其简单的,对于数 x,每次对 10 取余保存为结果,之后 x 除以 10,若 ...

  7. LeetCode 7. Reverse Integer

    问题链接 LeetCode 7 题目解析 给定一个32位有符号整数,求其反转数字. 解题思路 如果是简单反转的话,那这道题就太简单了.题目要求判断溢出问题,32位int类型的范围是-214748364 ...

  8. LeetCode——7. Reverse Integer

    一.题目链接:https://leetcode.com/problems/reverse-integer/ 二.题目大意: 给定一个整数,要求反转该整数之后再返回:如果归返回的整数超过了int型整数的 ...

  9. leetcode No7. Reverse Integer

    Question: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -32 ...

最新文章

  1. 掌声送给TensorFlow 2.0!用Keras搭建一个CNN | 入门教程
  2. 英文书《用unreal来学习c++》_用机器学习来提升你的用户增长:第四步,客户流失预测
  3. InnoDB redo log格式-物理log
  4. maven添加本地jar包依赖
  5. python 程序运行插件_如何使Python插件在Pluma中运行?
  6. springcloud(五):熔断监控Hystrix Dashboard和Turbine
  7. 连续投影算法_中航国画荣获“2020 IAV国际视听嘉年华” 大屏幕投影显示行业三大奖项!...
  8. [html] html元素哪些标签是不可替换元素?哪些是可替换元素?
  9. python降维之时间类型数据的处理_使用Python进行数据降维|线性降维
  10. ubuntu安装python_Linux下的Python开发配置鸭
  11. ESX/ESXi 主机上的每个插槽中安装了多少内存
  12. cni k8s 插件安装_实现K8S中Pod带宽限制
  13. 对 数组[i].index=i的理解
  14. 信息学奥赛一本通1001-1005
  15. 加拿大卡尔加里推出数字货币以促进当地经济发展
  16. Java 中status意思_struts的status属性
  17. 信创操作系统--麒麟Kylin桌面操作系统 (项目十二 使用Systemd管理系统服务)
  18. 微软时间服务器同步错误,Windows Server 设置时间同步出错问题
  19. 运行VBS权限受限,需要用Run as Administrator的cmd窗口来运行
  20. 一篇我觉得不错的文章

热门文章

  1. spark读取文件源码分析-1
  2. eclipse怎么导出一个Java项目(莫要错过,最详细教程!)
  3. 【已解决】报错:cannot be resolved to a variable
  4. [leetcode] 72.编辑距离
  5. [leetcode] 21.合并两个有序链表
  6. mac11.5.2版本虚拟机SeaBIOS不引导,kvm虚拟机状态为pause
  7. python online course_python-选课系统
  8. python自动下载app_APP自动化之安装Python(类库)环境
  9. FatFs最新版本获取方法
  10. RedHat虚拟机Vmware Tools的安装教程