经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 其他 » 职业生涯 » 查看文章
LeetCode 9. Palindrome Number(回文数)
来源:cnblogs  作者:flowingfog  时间:2018/10/12 9:29:53  对本文有异议

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

  1. Input: 121
  1. Output: true

Example 2:

  1. Input: -121
  1. Output: false
  1. Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

  1. Input: 10
  1. Output: false
  1. Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

  1. public boolean isPalindrome(int x) {
  2. int[] nums=new int[32];
  3. int i=0;
  4. if(x<0)//负数或 最后一位为0(后半句不严谨,单独0是对称的)
  5. return false;
  6. while(x/10>0){
  7. nums[i]=x%10;
  8. i++;
  9. x=x/10;
  10. }
  11. nums[i]=x%10;
  12. int len=0;
  13. for(i=31;i>=0;i--){
  14. if(nums[i]>0){
  15. len=i;//数字位数
  16. break;
  17. }
  18. }
  19. for(i=0;i<=len/2;i++){
  20. if(nums[i]!=nums[len-i])
  21. return false;
  22. }
  23. return true;
  24. }

 

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号