摘要:本文主要向大家介绍了JAVA语言关于Palindrome Number 的题目解答,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
本文主要向大家介绍了JAVA语言关于Palindrome Number 的题目解答,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
题目
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.确定整数是不是回文数,当整数向前和向后读相同的时候,就是回文数Example 1: Input: 121Output: trueExample 2: Input: -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.说明:从左向右为-121,从右向左为121-,所以它不是回文数Example 3: Input: 10Output: falseExplanation: Reads 01 from right to left. Therefore it is not a palindrome.说明:从右向左读为01,因此它不是回文数Follow up: Coud you solve it without converting the integer to a string你能不能将整数换为字符串?
代码class Solution { public boolean isPalindrome(int x) { if(x<0) {return false; } if(x==0) {return true; } StringBuilder in = new StringBuilder(); int a = x; while(a!=0) {in.append(a%10);a/=10; } String o = String.valueOf(x); if(o.equals(in.toString())) {return true; }else {return false; } }}
注意不要使用StringBuffer,StringBuffer在单线程下效率并不高。
StringBuffer为287ms
StringBuilder为99ms
可以看出StringBuffer在单线程下的重复加锁解锁操作耗费了大量时间
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号